From e5207e62314533523b322fb70b9b2fc450fb4467 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 27 Feb 2015 22:29:42 +0100 Subject: [PATCH] Polishing --- .../SpringConfiguredConfiguration.java | 11 +++--- .../AbstractCachingConfiguration.java | 38 ++++++++++--------- .../LoadTimeWeavingConfiguration.java | 24 ++++++------ .../annotation/MBeanExportConfiguration.java | 22 ++++++----- .../AbstractAsyncConfiguration.java | 11 +++--- ...actTransactionManagementConfiguration.java | 7 ++-- ...oxyTransactionManagementConfiguration.java | 8 ++-- .../DelegatingWebMvcConfiguration.java | 4 +- 8 files changed, 69 insertions(+), 56 deletions(-) diff --git a/spring-aspects/src/main/java/org/springframework/context/annotation/aspectj/SpringConfiguredConfiguration.java b/spring-aspects/src/main/java/org/springframework/context/annotation/aspectj/SpringConfiguredConfiguration.java index 8534fd76b5a..747f280f272 100644 --- a/spring-aspects/src/main/java/org/springframework/context/annotation/aspectj/SpringConfiguredConfiguration.java +++ b/spring-aspects/src/main/java/org/springframework/context/annotation/aspectj/SpringConfiguredConfiguration.java @@ -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"); * 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 * Configurable}. * - *

This configuration class is automatically imported when using the @{@link - * EnableSpringConfigured} annotation. See {@code @EnableSpringConfigured} Javadoc for - * complete usage details. + *

This configuration class is automatically imported when using the + * @{@link EnableSpringConfigured} annotation. See {@code @EnableSpringConfigured}'s + * javadoc for complete usage details. * * @author Chris Beams * @since 3.1 @@ -42,9 +42,10 @@ public class SpringConfiguredConfiguration { public static final String BEAN_CONFIGURER_ASPECT_BEAN_NAME = "org.springframework.context.config.internalBeanConfigurerAspect"; - @Bean(name=BEAN_CONFIGURER_ASPECT_BEAN_NAME) + @Bean(name = BEAN_CONFIGURER_ASPECT_BEAN_NAME) @Role(BeanDefinition.ROLE_INFRASTRUCTURE) public AnnotationBeanConfigurerAspect beanConfigurerAspect() { return AnnotationBeanConfigurerAspect.aspectOf(); } + } diff --git a/spring-context/src/main/java/org/springframework/cache/annotation/AbstractCachingConfiguration.java b/spring-context/src/main/java/org/springframework/cache/annotation/AbstractCachingConfiguration.java index 4937eeb5e38..71f55433374 100644 --- a/spring-context/src/main/java/org/springframework/cache/annotation/AbstractCachingConfiguration.java +++ b/spring-context/src/main/java/org/springframework/cache/annotation/AbstractCachingConfiguration.java @@ -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"); * you may not use this file except in compliance with the License. @@ -17,7 +17,6 @@ package org.springframework.cache.annotation; import java.util.Collection; - import javax.annotation.PostConstruct; 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.core.annotation.AnnotationAttributes; import org.springframework.core.type.AnnotationMetadata; -import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; /** - * Abstract base {@code @Configuration} class providing common structure for enabling - * Spring's annotation-driven cache management capability. + * Abstract base {@code @Configuration} class providing common structure + * for enabling Spring's annotation-driven cache management capability. * * @author Chris Beams * @since 3.1 @@ -42,22 +40,28 @@ import org.springframework.util.CollectionUtils; public abstract class AbstractCachingConfiguration implements ImportAware { protected AnnotationAttributes enableCaching; + protected CacheManager cacheManager; + protected KeyGenerator keyGenerator; - @Autowired(required=false) + @Autowired(required = false) private Collection cacheManagerBeans; - @Autowired(required=false) + + @Autowired(required = false) private Collection cachingConfigurers; + public void setImportMetadata(AnnotationMetadata importMetadata) { this.enableCaching = AnnotationAttributes.fromMap( importMetadata.getAnnotationAttributes(EnableCaching.class.getName(), false)); - Assert.notNull(this.enableCaching, - "@EnableCaching is not present on importing class " + - importMetadata.getClassName()); + if (this.enableCaching == null) { + throw new IllegalArgumentException( + "@EnableCaching is not present on importing class " + importMetadata.getClassName()); + } } + /** * Determine which {@code CacheManager} bean to use. Prefer the result of * {@link CachingConfigurer#cacheManager()} over any by-type matching. If none, fall @@ -68,20 +72,20 @@ public abstract class AbstractCachingConfiguration implements ImportAware { */ @PostConstruct protected void reconcileCacheManager() { - if (!CollectionUtils.isEmpty(cachingConfigurers)) { - int nConfigurers = cachingConfigurers.size(); + if (!CollectionUtils.isEmpty(this.cachingConfigurers)) { + int nConfigurers = this.cachingConfigurers.size(); if (nConfigurers > 1) { throw new IllegalStateException(nConfigurers + " implementations of " + "CachingConfigurer were found when only 1 was expected. " + "Refactor the configuration such that CachingConfigurer is " + "implemented only once or not at all."); } - CachingConfigurer cachingConfigurer = cachingConfigurers.iterator().next(); + CachingConfigurer cachingConfigurer = this.cachingConfigurers.iterator().next(); this.cacheManager = cachingConfigurer.cacheManager(); this.keyGenerator = cachingConfigurer.keyGenerator(); } - else if (!CollectionUtils.isEmpty(cacheManagerBeans)) { - int nManagers = cacheManagerBeans.size(); + else if (!CollectionUtils.isEmpty(this.cacheManagerBeans)) { + int nManagers = this.cacheManagerBeans.size(); if (nManagers > 1) { throw new IllegalStateException(nManagers + " beans of type CacheManager " + "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 " + "annotation-driven cache management."); } - CacheManager cacheManager = cacheManagerBeans.iterator().next(); - this.cacheManager = cacheManager; + this.cacheManager = cacheManager = this.cacheManagerBeans.iterator().next(); // keyGenerator remains null; will fall back to default within CacheInterceptor } else { @@ -99,4 +102,5 @@ public abstract class AbstractCachingConfiguration implements ImportAware { "from your configuration."); } } + } diff --git a/spring-context/src/main/java/org/springframework/context/annotation/LoadTimeWeavingConfiguration.java b/spring-context/src/main/java/org/springframework/context/annotation/LoadTimeWeavingConfiguration.java index d81821b7038..50d3ae81f7d 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/LoadTimeWeavingConfiguration.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/LoadTimeWeavingConfiguration.java @@ -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"); * 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.type.AnnotationMetadata; import org.springframework.instrument.classloading.LoadTimeWeaver; -import org.springframework.util.Assert; import static org.springframework.context.weaving.AspectJWeavingEnabler.*; /** * {@code @Configuration} class that registers a {@link LoadTimeWeaver} bean. * - *

This configuration class is automatically imported when using the @{@link - * EnableLoadTimeWeaving} annotation. See {@code @EnableLoadTimeWeaving} Javadoc for - * complete usage details. + *

This configuration class is automatically imported when using the + * {@link EnableLoadTimeWeaving} annotation. See {@code @EnableLoadTimeWeaving} + * javadoc for complete usage details. * * @author Chris Beams * @since 3.1 @@ -47,28 +46,31 @@ public class LoadTimeWeavingConfiguration implements ImportAware, BeanClassLoade private AnnotationAttributes enableLTW; - @Autowired(required=false) + @Autowired(required = false) private LoadTimeWeavingConfigurer ltwConfigurer; private ClassLoader beanClassLoader; + public void setImportMetadata(AnnotationMetadata importMetadata) { this.enableLTW = MetadataUtils.attributesFor(importMetadata, EnableLoadTimeWeaving.class); - Assert.notNull(this.enableLTW, - "@EnableLoadTimeWeaving is not present on importing class " + - importMetadata.getClassName()); + if (this.enableLTW == null) { + throw new IllegalArgumentException( + "@EnableLoadTimeWeaving is not present on importing class " + importMetadata.getClassName()); + } } public void setBeanClassLoader(ClassLoader beanClassLoader) { this.beanClassLoader = beanClassLoader; } - @Bean(name=ConfigurableApplicationContext.LOAD_TIME_WEAVER_BEAN_NAME) + + @Bean(name = ConfigurableApplicationContext.LOAD_TIME_WEAVER_BEAN_NAME) @Role(BeanDefinition.ROLE_INFRASTRUCTURE) public LoadTimeWeaver loadTimeWeaver() { LoadTimeWeaver loadTimeWeaver = null; - if (ltwConfigurer != null) { + if (this.ltwConfigurer != null) { // the user has provided a custom LTW instance loadTimeWeaver = ltwConfigurer.getLoadTimeWeaver(); } diff --git a/spring-context/src/main/java/org/springframework/context/annotation/MBeanExportConfiguration.java b/spring-context/src/main/java/org/springframework/context/annotation/MBeanExportConfiguration.java index 4528b38f0ea..223729cd19f 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/MBeanExportConfiguration.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/MBeanExportConfiguration.java @@ -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"); * 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.WebSphereMBeanServerFactoryBean; import org.springframework.jndi.JndiLocatorDelegate; -import org.springframework.util.Assert; import org.springframework.util.ClassUtils; 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 AnnotationAttributes attributes; + private AnnotationAttributes enableMBeanExport; private BeanFactory beanFactory; public void setImportMetadata(AnnotationMetadata importMetadata) { Map map = importMetadata.getAnnotationAttributes(EnableMBeanExport.class.getName()); - this.attributes = AnnotationAttributes.fromMap(map); - Assert.notNull(this.attributes, - "@EnableMBeanExport is not present on importing class " + importMetadata.getClassName()); + this.enableMBeanExport = AnnotationAttributes.fromMap(map); + if (this.enableMBeanExport == null) { + throw new IllegalArgumentException( + "@EnableMBeanExport is not present on importing class " + importMetadata.getClassName()); + } } public void setBeanFactory(BeanFactory beanFactory) { this.beanFactory = beanFactory; } - @Bean(name=MBEAN_EXPORTER_BEAN_NAME) + + @Bean(name = MBEAN_EXPORTER_BEAN_NAME) @Role(BeanDefinition.ROLE_INFRASTRUCTURE) public AnnotationMBeanExporter mbeanExporter() { AnnotationMBeanExporter exporter = new AnnotationMBeanExporter(); @@ -77,14 +79,14 @@ public class MBeanExportConfiguration implements ImportAware, BeanFactoryAware { } private void setupDomain(AnnotationMBeanExporter exporter) { - String defaultDomain = this.attributes.getString("defaultDomain"); + String defaultDomain = this.enableMBeanExport.getString("defaultDomain"); if (StringUtils.hasText(defaultDomain)) { exporter.setDefaultDomain(defaultDomain); } } private void setupServer(AnnotationMBeanExporter exporter) { - String server = this.attributes.getString("server"); + String server = this.enableMBeanExport.getString("server"); if (StringUtils.hasText(server)) { exporter.setServer(this.beanFactory.getBean(server, MBeanServer.class)); } @@ -97,7 +99,7 @@ public class MBeanExportConfiguration implements ImportAware, BeanFactoryAware { } private void setupRegistrationPolicy(AnnotationMBeanExporter exporter) { - RegistrationPolicy registrationPolicy = this.attributes.getEnum("registration"); + RegistrationPolicy registrationPolicy = this.enableMBeanExport.getEnum("registration"); exporter.setRegistrationPolicy(registrationPolicy); } diff --git a/spring-context/src/main/java/org/springframework/scheduling/annotation/AbstractAsyncConfiguration.java b/spring-context/src/main/java/org/springframework/scheduling/annotation/AbstractAsyncConfiguration.java index dce87b245c1..987c9fd16c5 100644 --- a/spring-context/src/main/java/org/springframework/scheduling/annotation/AbstractAsyncConfiguration.java +++ b/spring-context/src/main/java/org/springframework/scheduling/annotation/AbstractAsyncConfiguration.java @@ -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"); * 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.core.annotation.AnnotationAttributes; import org.springframework.core.type.AnnotationMetadata; -import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; /** @@ -46,14 +45,16 @@ public abstract class AbstractAsyncConfiguration implements ImportAware { public void setImportMetadata(AnnotationMetadata importMetadata) { this.enableAsync = AnnotationAttributes.fromMap( importMetadata.getAnnotationAttributes(EnableAsync.class.getName(), false)); - Assert.notNull(this.enableAsync, - "@EnableAsync is not present on importing class " + importMetadata.getClassName()); + if (this.enableAsync == null) { + throw new IllegalArgumentException( + "@EnableAsync is not present on importing class " + importMetadata.getClassName()); + } } /** * Collect any {@link AsyncConfigurer} beans through autowiring. */ - @Autowired(required=false) + @Autowired(required = false) void setConfigurers(Collection configurers) { if (CollectionUtils.isEmpty(configurers)) { return; diff --git a/spring-tx/src/main/java/org/springframework/transaction/annotation/AbstractTransactionManagementConfiguration.java b/spring-tx/src/main/java/org/springframework/transaction/annotation/AbstractTransactionManagementConfiguration.java index 52690fadada..970693f9ccd 100644 --- a/spring-tx/src/main/java/org/springframework/transaction/annotation/AbstractTransactionManagementConfiguration.java +++ b/spring-tx/src/main/java/org/springframework/transaction/annotation/AbstractTransactionManagementConfiguration.java @@ -24,7 +24,6 @@ import org.springframework.context.annotation.ImportAware; import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.type.AnnotationMetadata; import org.springframework.transaction.PlatformTransactionManager; -import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; /** @@ -49,8 +48,10 @@ public abstract class AbstractTransactionManagementConfiguration implements Impo public void setImportMetadata(AnnotationMetadata importMetadata) { this.enableTx = AnnotationAttributes.fromMap( importMetadata.getAnnotationAttributes(EnableTransactionManagement.class.getName(), false)); - Assert.notNull(this.enableTx, - "@EnableTransactionManagement is not present on importing class " + importMetadata.getClassName()); + if (this.enableTx == null) { + throw new IllegalArgumentException( + "@EnableTransactionManagement is not present on importing class " + importMetadata.getClassName()); + } } @Autowired(required = false) diff --git a/spring-tx/src/main/java/org/springframework/transaction/annotation/ProxyTransactionManagementConfiguration.java b/spring-tx/src/main/java/org/springframework/transaction/annotation/ProxyTransactionManagementConfiguration.java index 40a86373456..616551c67b6 100644 --- a/spring-tx/src/main/java/org/springframework/transaction/annotation/ProxyTransactionManagementConfiguration.java +++ b/spring-tx/src/main/java/org/springframework/transaction/annotation/ProxyTransactionManagementConfiguration.java @@ -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"); * 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; /** - * {@code @Configuration} class that registers the Spring infrastructure beans necessary - * to enable proxy-based annotation-driven transaction management. + * {@code @Configuration} class that registers the Spring infrastructure beans + * necessary to enable proxy-based annotation-driven transaction management. * * @author Chris Beams * @since 3.1 @@ -37,7 +37,7 @@ import org.springframework.transaction.interceptor.TransactionInterceptor; @Configuration public class ProxyTransactionManagementConfiguration extends AbstractTransactionManagementConfiguration { - @Bean(name=TransactionManagementConfigUtils.TRANSACTION_ADVISOR_BEAN_NAME) + @Bean(name = TransactionManagementConfigUtils.TRANSACTION_ADVISOR_BEAN_NAME) @Role(BeanDefinition.ROLE_INFRASTRUCTURE) public BeanFactoryTransactionAttributeSourceAdvisor transactionAdvisor() { BeanFactoryTransactionAttributeSourceAdvisor advisor = new BeanFactoryTransactionAttributeSourceAdvisor(); diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.java index d9d58035b17..579de783eb3 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.java @@ -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"); * 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(); + @Autowired(required = false) public void setConfigurers(List configurers) { if (configurers == null || configurers.isEmpty()) { @@ -50,6 +51,7 @@ public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport { this.configurers.addWebMvcConfigurers(configurers); } + @Override protected void addInterceptors(InterceptorRegistry registry) { this.configurers.addInterceptors(registry);