Polishing

This commit is contained in:
Juergen Hoeller 2015-02-27 22:29:42 +01:00
parent 1c10861a92
commit e5207e6231
8 changed files with 69 additions and 56 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -28,9 +28,9 @@ import org.springframework.context.annotation.Role;
* annotated with @{@link org.springframework.beans.factory.annotation.Configurable * annotated with @{@link org.springframework.beans.factory.annotation.Configurable
* Configurable}. * Configurable}.
* *
* <p>This configuration class is automatically imported when using the @{@link * <p>This configuration class is automatically imported when using the
* EnableSpringConfigured} annotation. See {@code @EnableSpringConfigured} Javadoc for * @{@link EnableSpringConfigured} annotation. See {@code @EnableSpringConfigured}'s
* complete usage details. * javadoc for complete usage details.
* *
* @author Chris Beams * @author Chris Beams
* @since 3.1 * @since 3.1
@ -42,9 +42,10 @@ public class SpringConfiguredConfiguration {
public static final String BEAN_CONFIGURER_ASPECT_BEAN_NAME = public static final String BEAN_CONFIGURER_ASPECT_BEAN_NAME =
"org.springframework.context.config.internalBeanConfigurerAspect"; "org.springframework.context.config.internalBeanConfigurerAspect";
@Bean(name=BEAN_CONFIGURER_ASPECT_BEAN_NAME) @Bean(name = BEAN_CONFIGURER_ASPECT_BEAN_NAME)
@Role(BeanDefinition.ROLE_INFRASTRUCTURE) @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public AnnotationBeanConfigurerAspect beanConfigurerAspect() { public AnnotationBeanConfigurerAspect beanConfigurerAspect() {
return AnnotationBeanConfigurerAspect.aspectOf(); return AnnotationBeanConfigurerAspect.aspectOf();
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,7 +17,6 @@
package org.springframework.cache.annotation; package org.springframework.cache.annotation;
import java.util.Collection; import java.util.Collection;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -27,12 +26,11 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportAware; import org.springframework.context.annotation.ImportAware;
import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata; import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
/** /**
* Abstract base {@code @Configuration} class providing common structure for enabling * Abstract base {@code @Configuration} class providing common structure
* Spring's annotation-driven cache management capability. * for enabling Spring's annotation-driven cache management capability.
* *
* @author Chris Beams * @author Chris Beams
* @since 3.1 * @since 3.1
@ -42,22 +40,28 @@ import org.springframework.util.CollectionUtils;
public abstract class AbstractCachingConfiguration implements ImportAware { public abstract class AbstractCachingConfiguration implements ImportAware {
protected AnnotationAttributes enableCaching; protected AnnotationAttributes enableCaching;
protected CacheManager cacheManager; protected CacheManager cacheManager;
protected KeyGenerator keyGenerator; protected KeyGenerator keyGenerator;
@Autowired(required=false) @Autowired(required = false)
private Collection<CacheManager> cacheManagerBeans; private Collection<CacheManager> cacheManagerBeans;
@Autowired(required=false)
@Autowired(required = false)
private Collection<CachingConfigurer> cachingConfigurers; private Collection<CachingConfigurer> cachingConfigurers;
public void setImportMetadata(AnnotationMetadata importMetadata) { public void setImportMetadata(AnnotationMetadata importMetadata) {
this.enableCaching = AnnotationAttributes.fromMap( this.enableCaching = AnnotationAttributes.fromMap(
importMetadata.getAnnotationAttributes(EnableCaching.class.getName(), false)); importMetadata.getAnnotationAttributes(EnableCaching.class.getName(), false));
Assert.notNull(this.enableCaching, if (this.enableCaching == null) {
"@EnableCaching is not present on importing class " + throw new IllegalArgumentException(
importMetadata.getClassName()); "@EnableCaching is not present on importing class " + importMetadata.getClassName());
}
} }
/** /**
* Determine which {@code CacheManager} bean to use. Prefer the result of * Determine which {@code CacheManager} bean to use. Prefer the result of
* {@link CachingConfigurer#cacheManager()} over any by-type matching. If none, fall * {@link CachingConfigurer#cacheManager()} over any by-type matching. If none, fall
@ -68,20 +72,20 @@ public abstract class AbstractCachingConfiguration implements ImportAware {
*/ */
@PostConstruct @PostConstruct
protected void reconcileCacheManager() { protected void reconcileCacheManager() {
if (!CollectionUtils.isEmpty(cachingConfigurers)) { if (!CollectionUtils.isEmpty(this.cachingConfigurers)) {
int nConfigurers = cachingConfigurers.size(); int nConfigurers = this.cachingConfigurers.size();
if (nConfigurers > 1) { if (nConfigurers > 1) {
throw new IllegalStateException(nConfigurers + " implementations of " + throw new IllegalStateException(nConfigurers + " implementations of " +
"CachingConfigurer were found when only 1 was expected. " + "CachingConfigurer were found when only 1 was expected. " +
"Refactor the configuration such that CachingConfigurer is " + "Refactor the configuration such that CachingConfigurer is " +
"implemented only once or not at all."); "implemented only once or not at all.");
} }
CachingConfigurer cachingConfigurer = cachingConfigurers.iterator().next(); CachingConfigurer cachingConfigurer = this.cachingConfigurers.iterator().next();
this.cacheManager = cachingConfigurer.cacheManager(); this.cacheManager = cachingConfigurer.cacheManager();
this.keyGenerator = cachingConfigurer.keyGenerator(); this.keyGenerator = cachingConfigurer.keyGenerator();
} }
else if (!CollectionUtils.isEmpty(cacheManagerBeans)) { else if (!CollectionUtils.isEmpty(this.cacheManagerBeans)) {
int nManagers = cacheManagerBeans.size(); int nManagers = this.cacheManagerBeans.size();
if (nManagers > 1) { if (nManagers > 1) {
throw new IllegalStateException(nManagers + " beans of type CacheManager " + throw new IllegalStateException(nManagers + " beans of type CacheManager " +
"were found when only 1 was expected. Remove all but one of the " + "were found when only 1 was expected. Remove all but one of the " +
@ -89,8 +93,7 @@ public abstract class AbstractCachingConfiguration implements ImportAware {
"to make explicit which CacheManager should be used for " + "to make explicit which CacheManager should be used for " +
"annotation-driven cache management."); "annotation-driven cache management.");
} }
CacheManager cacheManager = cacheManagerBeans.iterator().next(); this.cacheManager = cacheManager = this.cacheManagerBeans.iterator().next();
this.cacheManager = cacheManager;
// keyGenerator remains null; will fall back to default within CacheInterceptor // keyGenerator remains null; will fall back to default within CacheInterceptor
} }
else { else {
@ -99,4 +102,5 @@ public abstract class AbstractCachingConfiguration implements ImportAware {
"from your configuration."); "from your configuration.");
} }
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2011 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -26,16 +26,15 @@ import org.springframework.context.weaving.DefaultContextLoadTimeWeaver;
import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata; import org.springframework.core.type.AnnotationMetadata;
import org.springframework.instrument.classloading.LoadTimeWeaver; import org.springframework.instrument.classloading.LoadTimeWeaver;
import org.springframework.util.Assert;
import static org.springframework.context.weaving.AspectJWeavingEnabler.*; import static org.springframework.context.weaving.AspectJWeavingEnabler.*;
/** /**
* {@code @Configuration} class that registers a {@link LoadTimeWeaver} bean. * {@code @Configuration} class that registers a {@link LoadTimeWeaver} bean.
* *
* <p>This configuration class is automatically imported when using the @{@link * <p>This configuration class is automatically imported when using the
* EnableLoadTimeWeaving} annotation. See {@code @EnableLoadTimeWeaving} Javadoc for * {@link EnableLoadTimeWeaving} annotation. See {@code @EnableLoadTimeWeaving}
* complete usage details. * javadoc for complete usage details.
* *
* @author Chris Beams * @author Chris Beams
* @since 3.1 * @since 3.1
@ -47,28 +46,31 @@ public class LoadTimeWeavingConfiguration implements ImportAware, BeanClassLoade
private AnnotationAttributes enableLTW; private AnnotationAttributes enableLTW;
@Autowired(required=false) @Autowired(required = false)
private LoadTimeWeavingConfigurer ltwConfigurer; private LoadTimeWeavingConfigurer ltwConfigurer;
private ClassLoader beanClassLoader; private ClassLoader beanClassLoader;
public void setImportMetadata(AnnotationMetadata importMetadata) { public void setImportMetadata(AnnotationMetadata importMetadata) {
this.enableLTW = MetadataUtils.attributesFor(importMetadata, EnableLoadTimeWeaving.class); this.enableLTW = MetadataUtils.attributesFor(importMetadata, EnableLoadTimeWeaving.class);
Assert.notNull(this.enableLTW, if (this.enableLTW == null) {
"@EnableLoadTimeWeaving is not present on importing class " + throw new IllegalArgumentException(
importMetadata.getClassName()); "@EnableLoadTimeWeaving is not present on importing class " + importMetadata.getClassName());
}
} }
public void setBeanClassLoader(ClassLoader beanClassLoader) { public void setBeanClassLoader(ClassLoader beanClassLoader) {
this.beanClassLoader = beanClassLoader; this.beanClassLoader = beanClassLoader;
} }
@Bean(name=ConfigurableApplicationContext.LOAD_TIME_WEAVER_BEAN_NAME)
@Bean(name = ConfigurableApplicationContext.LOAD_TIME_WEAVER_BEAN_NAME)
@Role(BeanDefinition.ROLE_INFRASTRUCTURE) @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public LoadTimeWeaver loadTimeWeaver() { public LoadTimeWeaver loadTimeWeaver() {
LoadTimeWeaver loadTimeWeaver = null; LoadTimeWeaver loadTimeWeaver = null;
if (ltwConfigurer != null) { if (this.ltwConfigurer != null) {
// the user has provided a custom LTW instance // the user has provided a custom LTW instance
loadTimeWeaver = ltwConfigurer.getLoadTimeWeaver(); loadTimeWeaver = ltwConfigurer.getLoadTimeWeaver();
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -30,7 +30,6 @@ import org.springframework.jmx.export.annotation.AnnotationMBeanExporter;
import org.springframework.jmx.support.RegistrationPolicy; import org.springframework.jmx.support.RegistrationPolicy;
import org.springframework.jmx.support.WebSphereMBeanServerFactoryBean; import org.springframework.jmx.support.WebSphereMBeanServerFactoryBean;
import org.springframework.jndi.JndiLocatorDelegate; import org.springframework.jndi.JndiLocatorDelegate;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -50,23 +49,26 @@ public class MBeanExportConfiguration implements ImportAware, BeanFactoryAware {
private static final String MBEAN_EXPORTER_BEAN_NAME = "mbeanExporter"; private static final String MBEAN_EXPORTER_BEAN_NAME = "mbeanExporter";
private AnnotationAttributes attributes; private AnnotationAttributes enableMBeanExport;
private BeanFactory beanFactory; private BeanFactory beanFactory;
public void setImportMetadata(AnnotationMetadata importMetadata) { public void setImportMetadata(AnnotationMetadata importMetadata) {
Map<String, Object> map = importMetadata.getAnnotationAttributes(EnableMBeanExport.class.getName()); Map<String, Object> map = importMetadata.getAnnotationAttributes(EnableMBeanExport.class.getName());
this.attributes = AnnotationAttributes.fromMap(map); this.enableMBeanExport = AnnotationAttributes.fromMap(map);
Assert.notNull(this.attributes, if (this.enableMBeanExport == null) {
"@EnableMBeanExport is not present on importing class " + importMetadata.getClassName()); throw new IllegalArgumentException(
"@EnableMBeanExport is not present on importing class " + importMetadata.getClassName());
}
} }
public void setBeanFactory(BeanFactory beanFactory) { public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory; this.beanFactory = beanFactory;
} }
@Bean(name=MBEAN_EXPORTER_BEAN_NAME)
@Bean(name = MBEAN_EXPORTER_BEAN_NAME)
@Role(BeanDefinition.ROLE_INFRASTRUCTURE) @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public AnnotationMBeanExporter mbeanExporter() { public AnnotationMBeanExporter mbeanExporter() {
AnnotationMBeanExporter exporter = new AnnotationMBeanExporter(); AnnotationMBeanExporter exporter = new AnnotationMBeanExporter();
@ -77,14 +79,14 @@ public class MBeanExportConfiguration implements ImportAware, BeanFactoryAware {
} }
private void setupDomain(AnnotationMBeanExporter exporter) { private void setupDomain(AnnotationMBeanExporter exporter) {
String defaultDomain = this.attributes.getString("defaultDomain"); String defaultDomain = this.enableMBeanExport.getString("defaultDomain");
if (StringUtils.hasText(defaultDomain)) { if (StringUtils.hasText(defaultDomain)) {
exporter.setDefaultDomain(defaultDomain); exporter.setDefaultDomain(defaultDomain);
} }
} }
private void setupServer(AnnotationMBeanExporter exporter) { private void setupServer(AnnotationMBeanExporter exporter) {
String server = this.attributes.getString("server"); String server = this.enableMBeanExport.getString("server");
if (StringUtils.hasText(server)) { if (StringUtils.hasText(server)) {
exporter.setServer(this.beanFactory.getBean(server, MBeanServer.class)); exporter.setServer(this.beanFactory.getBean(server, MBeanServer.class));
} }
@ -97,7 +99,7 @@ public class MBeanExportConfiguration implements ImportAware, BeanFactoryAware {
} }
private void setupRegistrationPolicy(AnnotationMBeanExporter exporter) { private void setupRegistrationPolicy(AnnotationMBeanExporter exporter) {
RegistrationPolicy registrationPolicy = this.attributes.getEnum("registration"); RegistrationPolicy registrationPolicy = this.enableMBeanExport.getEnum("registration");
exporter.setRegistrationPolicy(registrationPolicy); exporter.setRegistrationPolicy(registrationPolicy);
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -24,7 +24,6 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportAware; import org.springframework.context.annotation.ImportAware;
import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata; import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
/** /**
@ -46,14 +45,16 @@ public abstract class AbstractAsyncConfiguration implements ImportAware {
public void setImportMetadata(AnnotationMetadata importMetadata) { public void setImportMetadata(AnnotationMetadata importMetadata) {
this.enableAsync = AnnotationAttributes.fromMap( this.enableAsync = AnnotationAttributes.fromMap(
importMetadata.getAnnotationAttributes(EnableAsync.class.getName(), false)); importMetadata.getAnnotationAttributes(EnableAsync.class.getName(), false));
Assert.notNull(this.enableAsync, if (this.enableAsync == null) {
"@EnableAsync is not present on importing class " + importMetadata.getClassName()); throw new IllegalArgumentException(
"@EnableAsync is not present on importing class " + importMetadata.getClassName());
}
} }
/** /**
* Collect any {@link AsyncConfigurer} beans through autowiring. * Collect any {@link AsyncConfigurer} beans through autowiring.
*/ */
@Autowired(required=false) @Autowired(required = false)
void setConfigurers(Collection<AsyncConfigurer> configurers) { void setConfigurers(Collection<AsyncConfigurer> configurers) {
if (CollectionUtils.isEmpty(configurers)) { if (CollectionUtils.isEmpty(configurers)) {
return; return;

View File

@ -24,7 +24,6 @@ import org.springframework.context.annotation.ImportAware;
import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata; import org.springframework.core.type.AnnotationMetadata;
import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
/** /**
@ -49,8 +48,10 @@ public abstract class AbstractTransactionManagementConfiguration implements Impo
public void setImportMetadata(AnnotationMetadata importMetadata) { public void setImportMetadata(AnnotationMetadata importMetadata) {
this.enableTx = AnnotationAttributes.fromMap( this.enableTx = AnnotationAttributes.fromMap(
importMetadata.getAnnotationAttributes(EnableTransactionManagement.class.getName(), false)); importMetadata.getAnnotationAttributes(EnableTransactionManagement.class.getName(), false));
Assert.notNull(this.enableTx, if (this.enableTx == null) {
"@EnableTransactionManagement is not present on importing class " + importMetadata.getClassName()); throw new IllegalArgumentException(
"@EnableTransactionManagement is not present on importing class " + importMetadata.getClassName());
}
} }
@Autowired(required = false) @Autowired(required = false)

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -26,8 +26,8 @@ import org.springframework.transaction.interceptor.TransactionAttributeSource;
import org.springframework.transaction.interceptor.TransactionInterceptor; import org.springframework.transaction.interceptor.TransactionInterceptor;
/** /**
* {@code @Configuration} class that registers the Spring infrastructure beans necessary * {@code @Configuration} class that registers the Spring infrastructure beans
* to enable proxy-based annotation-driven transaction management. * necessary to enable proxy-based annotation-driven transaction management.
* *
* @author Chris Beams * @author Chris Beams
* @since 3.1 * @since 3.1
@ -37,7 +37,7 @@ import org.springframework.transaction.interceptor.TransactionInterceptor;
@Configuration @Configuration
public class ProxyTransactionManagementConfiguration extends AbstractTransactionManagementConfiguration { public class ProxyTransactionManagementConfiguration extends AbstractTransactionManagementConfiguration {
@Bean(name=TransactionManagementConfigUtils.TRANSACTION_ADVISOR_BEAN_NAME) @Bean(name = TransactionManagementConfigUtils.TRANSACTION_ADVISOR_BEAN_NAME)
@Role(BeanDefinition.ROLE_INFRASTRUCTURE) @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public BeanFactoryTransactionAttributeSourceAdvisor transactionAdvisor() { public BeanFactoryTransactionAttributeSourceAdvisor transactionAdvisor() {
BeanFactoryTransactionAttributeSourceAdvisor advisor = new BeanFactoryTransactionAttributeSourceAdvisor(); BeanFactoryTransactionAttributeSourceAdvisor advisor = new BeanFactoryTransactionAttributeSourceAdvisor();

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -42,6 +42,7 @@ public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
private final WebMvcConfigurerComposite configurers = new WebMvcConfigurerComposite(); private final WebMvcConfigurerComposite configurers = new WebMvcConfigurerComposite();
@Autowired(required = false) @Autowired(required = false)
public void setConfigurers(List<WebMvcConfigurer> configurers) { public void setConfigurers(List<WebMvcConfigurer> configurers) {
if (configurers == null || configurers.isEmpty()) { if (configurers == null || configurers.isEmpty()) {
@ -50,6 +51,7 @@ public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
this.configurers.addWebMvcConfigurers(configurers); this.configurers.addWebMvcConfigurers(configurers);
} }
@Override @Override
protected void addInterceptors(InterceptorRegistry registry) { protected void addInterceptors(InterceptorRegistry registry) {
this.configurers.addInterceptors(registry); this.configurers.addInterceptors(registry);