From 5639eb66b69ec01dcce74245e89df45084716183 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 2 Dec 2013 16:32:02 +0100 Subject: [PATCH] Polishing --- .../aop/scope/ScopedProxyUtils.java | 4 +- .../ConfigurationClassEnhancer.java | 37 +++++++++-------- .../AbstractAsyncConfiguration.java | 40 ++++++++++--------- .../core/env/StandardEnvironment.java | 12 +++--- .../support/AbstractContextLoader.java | 13 +++--- ...actTransactionManagementConfiguration.java | 14 +++---- ...actionManagementConfigurationSelector.java | 7 ++-- 7 files changed, 63 insertions(+), 64 deletions(-) diff --git a/spring-aop/src/main/java/org/springframework/aop/scope/ScopedProxyUtils.java b/spring-aop/src/main/java/org/springframework/aop/scope/ScopedProxyUtils.java index 0a3e5ba893b..ce4f4e926b0 100644 --- a/spring-aop/src/main/java/org/springframework/aop/scope/ScopedProxyUtils.java +++ b/spring-aop/src/main/java/org/springframework/aop/scope/ScopedProxyUtils.java @@ -38,7 +38,7 @@ public abstract class ScopedProxyUtils { /** - * Generates a scoped proxy for the supplied target bean, registering the target + * Generate a scoped proxy for the supplied target bean, registering the target * bean with an internal name and setting 'targetBeanName' on the scoped proxy. * @param definition the original bean definition * @param registry the bean definition registry @@ -63,7 +63,7 @@ public abstract class ScopedProxyUtils { if (proxyTargetClass) { targetDefinition.setAttribute(AutoProxyUtils.PRESERVE_TARGET_CLASS_ATTRIBUTE, Boolean.TRUE); - // ScopedFactoryBean's "proxyTargetClass" default is TRUE, so we don't need to set it explicitly here. + // ScopedProxyFactoryBean's "proxyTargetClass" default is TRUE, so we don't need to set it explicitly here. } else { proxyDefinition.getPropertyValues().add("proxyTargetClass", Boolean.FALSE); diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassEnhancer.java b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassEnhancer.java index a74deefd070..0f1b97f4824 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassEnhancer.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassEnhancer.java @@ -50,15 +50,15 @@ import org.springframework.util.Assert; */ class ConfigurationClassEnhancer { - private static final Log logger = LogFactory.getLog(ConfigurationClassEnhancer.class); - - private static final CallbackFilter CALLBACK_FILTER = new ConfigurationClassCallbackFilter(); - private static final Callback DISPOSABLE_BEAN_METHOD_INTERCEPTOR = new DisposableBeanMethodInterceptor(); private static final Class[] CALLBACK_TYPES = {BeanMethodInterceptor.class, DisposableBeanMethodInterceptor.class, NoOp.class}; + private static final CallbackFilter CALLBACK_FILTER = new ConfigurationClassCallbackFilter(); + + private static final Log logger = LogFactory.getLog(ConfigurationClassEnhancer.class); + private final Callback[] callbackInstances; @@ -103,7 +103,7 @@ class ConfigurationClassEnhancer { private Enhancer newEnhancer(Class superclass) { Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(superclass); - enhancer.setInterfaces(new Class[] {EnhancedConfiguration.class}); + enhancer.setInterfaces(new Class[] {EnhancedConfiguration.class}); enhancer.setUseFactory(false); enhancer.setCallbackFilter(CALLBACK_FILTER); enhancer.setCallbackTypes(CALLBACK_TYPES); @@ -116,13 +116,13 @@ class ConfigurationClassEnhancer { */ private Class createClass(Enhancer enhancer) { Class subclass = enhancer.createClass(); - // registering callbacks statically (as opposed to threadlocal) is critical for usage in an OSGi env (SPR-5932) + // Registering callbacks statically (as opposed to thread-local) + // is critical for usage in an OSGi environment (SPR-5932)... Enhancer.registerStaticCallbacks(subclass, this.callbackInstances); return subclass; } - /** * Marker interface to be implemented by all @Configuration CGLIB subclasses. * Facilitates idempotent behavior for {@link ConfigurationClassEnhancer#enhance(Class)} @@ -192,8 +192,8 @@ class ConfigurationClassEnhancer { public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { Enhancer.registerStaticCallbacks(obj.getClass(), null); - // does the actual (non-CGLIB) superclass actually implement DisposableBean? - // if so, call its dispose() method. If not, just exit. + // Does the actual (non-CGLIB) superclass actually implement DisposableBean? + // If so, call its dispose() method. If not, just exit. if (DisposableBean.class.isAssignableFrom(obj.getClass().getSuperclass())) { return proxy.invokeSuper(obj, args); } @@ -242,7 +242,7 @@ class ConfigurationClassEnhancer { String beanName = BeanAnnotationHelper.determineBeanNameFor(beanMethod); - // determine whether this bean is a scoped-proxy + // Determine whether this bean is a scoped-proxy Scope scope = AnnotationUtils.findAnnotation(beanMethod, Scope.class); if (scope != null && scope.proxyMode() != ScopedProxyMode.NO) { String scopedBeanName = ScopedProxyCreator.getTargetBeanName(beanName); @@ -251,27 +251,27 @@ class ConfigurationClassEnhancer { } } - // to handle the case of an inter-bean method reference, we must explicitly check the - // container for already cached instances + // To handle the case of an inter-bean method reference, we must explicitly check the + // container for already cached instances. - // first, check to see if the requested bean is a FactoryBean. If so, create a subclass + // First, check to see if the requested bean is a FactoryBean. If so, create a subclass // proxy that intercepts calls to getObject() and returns any cached bean instance. - // this ensures that the semantics of calling a FactoryBean from within @Bean methods + // This ensures that the semantics of calling a FactoryBean from within @Bean methods // is the same as that of referring to a FactoryBean within XML. See SPR-6602. if (factoryContainsBean(BeanFactory.FACTORY_BEAN_PREFIX + beanName) && factoryContainsBean(beanName)) { Object factoryBean = this.beanFactory.getBean(BeanFactory.FACTORY_BEAN_PREFIX + beanName); if (factoryBean instanceof ScopedProxyFactoryBean) { - // pass through - scoped proxy factory beans are a special case and should not + // Pass through - scoped proxy factory beans are a special case and should not // be further proxied } else { - // it is a candidate FactoryBean - go ahead with enhancement + // It is a candidate FactoryBean - go ahead with enhancement return enhanceFactoryBean(factoryBean.getClass(), beanName); } } if (isCurrentlyInvokedFactoryMethod(beanMethod) && !this.beanFactory.containsSingleton(beanName)) { - // the factory is calling the bean method in order to instantiate and register the bean + // The factory is calling the bean method in order to instantiate and register the bean // (i.e. via a getBean() call) -> invoke the super implementation of the method to actually // create the bean instance. if (BeanFactoryPostProcessor.class.isAssignableFrom(beanMethod.getReturnType())) { @@ -286,7 +286,7 @@ class ConfigurationClassEnhancer { return cglibMethodProxy.invokeSuper(enhancedConfigInstance, beanMethodArgs); } else { - // the user (i.e. not the factory) is requesting this bean through a + // The user (i.e. not the factory) is requesting this bean through a // call to the bean method, direct or indirect. The bean may have already been // marked as 'in creation' in certain autowiring scenarios; if so, temporarily // set the in-creation status to false in order to avoid an exception. @@ -303,7 +303,6 @@ class ConfigurationClassEnhancer { } } } - } /** 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 57fd7b69452..7b306e8d9af 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-2012 the original author or authors. + * Copyright 2002-2013 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. @@ -25,6 +25,7 @@ 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 @@ -38,16 +39,33 @@ import org.springframework.util.Assert; public abstract class AbstractAsyncConfiguration implements ImportAware { protected AnnotationAttributes enableAsync; + protected Executor executor; + 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()); + "@EnableAsync is not present on importing class " + importMetadata.getClassName()); } + /** + * Collect any {@link AsyncConfigurer} beans through autowiring. + */ + @Autowired(required = false) + void setConfigurers(Collection configurers) { + if (CollectionUtils.isEmpty(configurers)) { + return; + } + if (configurers.size() > 1) { + throw new IllegalStateException("Only one AsyncConfigurer may exist"); + } + AsyncConfigurer configurer = configurers.iterator().next(); + this.executor = configurer.getAsyncExecutor(); + } + + /** * The component that will apply async execution advice to beans annotated with * the async annotation. Subclasses will provide either a BeanPostProcessor in @@ -55,20 +73,4 @@ public abstract class AbstractAsyncConfiguration implements ImportAware { */ public abstract Object asyncAdvisor(); - /** - * Collect any {@link AsyncConfigurer} beans through autowiring. - */ - @Autowired(required = false) - void setConfigurers(Collection configurers) { - if (configurers == null || configurers.isEmpty()) { - return; - } - - if (configurers.size() > 1) { - throw new IllegalStateException("only one AsyncConfigurer may exist"); - } - - AsyncConfigurer configurer = configurers.iterator().next(); - this.executor = configurer.getAsyncExecutor(); - } } diff --git a/spring-core/src/main/java/org/springframework/core/env/StandardEnvironment.java b/spring-core/src/main/java/org/springframework/core/env/StandardEnvironment.java index 35b97e5b856..9437134f91a 100644 --- a/spring-core/src/main/java/org/springframework/core/env/StandardEnvironment.java +++ b/spring-core/src/main/java/org/springframework/core/env/StandardEnvironment.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2013 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,8 +24,8 @@ package org.springframework.core.env; * property resolution and profile-related operations, this implementation configures two * default property sources, to be searched in the following order: *
    - *
  • {@linkplain AbstractEnvironment#getSystemProperties() system properties} - *
  • {@linkplain AbstractEnvironment#getSystemEnvironment() system environment variables} + *
  • {@linkplain AbstractEnvironment#getSystemProperties() system properties} + *
  • {@linkplain AbstractEnvironment#getSystemEnvironment() system environment variables} *
* * That is, if the key "xyz" is present both in the JVM system properties as well as in @@ -41,7 +41,7 @@ package org.springframework.core.env; * instance available from {@link #getPropertySources()}. See * {@link ConfigurableEnvironment} Javadoc for usage examples. * - *

See {@link SystemEnvironmentPropertySource} Javadoc for details on special handling + *

See {@link SystemEnvironmentPropertySource} javadoc for details on special handling * of property names in shell environments (e.g. Bash) that disallow period characters in * variable names. * @@ -61,8 +61,8 @@ public class StandardEnvironment extends AbstractEnvironment { /** - * Customize the set of property sources with those appropriate for any standard Java - * environment: + * Customize the set of property sources with those appropriate for any standard + * Java environment: *

    *
  • {@value #SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME} *
  • {@value #SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME} diff --git a/spring-test/src/main/java/org/springframework/test/context/support/AbstractContextLoader.java b/spring-test/src/main/java/org/springframework/test/context/support/AbstractContextLoader.java index 0fc6c6012c7..51c6369d79f 100644 --- a/spring-test/src/main/java/org/springframework/test/context/support/AbstractContextLoader.java +++ b/spring-test/src/main/java/org/springframework/test/context/support/AbstractContextLoader.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2013 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,12 +17,12 @@ package org.springframework.test.context.support; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.Set; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; + import org.springframework.beans.BeanUtils; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextInitializer; @@ -120,12 +120,11 @@ public abstract class AbstractContextLoader implements SmartContextLoader { */ @SuppressWarnings("unchecked") protected void prepareContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) { - context.getEnvironment().setActiveProfiles(mergedConfig.getActiveProfiles()); - Set>> initializerClasses = mergedConfig.getContextInitializerClasses(); - - if (initializerClasses.size() == 0) { + Set>> initializerClasses = + mergedConfig.getContextInitializerClasses(); + if (initializerClasses.isEmpty()) { // no ApplicationContextInitializers have been declared -> nothing to do return; } @@ -144,7 +143,7 @@ public abstract class AbstractContextLoader implements SmartContextLoader { initializerInstances.add((ApplicationContextInitializer) BeanUtils.instantiateClass(initializerClass)); } - Collections.sort(initializerInstances, new AnnotationAwareOrderComparator()); + AnnotationAwareOrderComparator.sort(initializerInstances); for (ApplicationContextInitializer initializer : initializerInstances) { initializer.initialize(context); } 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 d5ea077155f..4d2aa5be591 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2013 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. @@ -25,6 +25,7 @@ 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; /** * Abstract base {@code @Configuration} class providing common structure for enabling @@ -38,26 +39,25 @@ import org.springframework.util.Assert; public abstract class AbstractTransactionManagementConfiguration implements ImportAware { protected AnnotationAttributes enableTx; + protected PlatformTransactionManager txManager; + 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()); + "@EnableTransactionManagement is not present on importing class " + importMetadata.getClassName()); } @Autowired(required=false) void setConfigurers(Collection configurers) { - if (configurers == null || configurers.isEmpty()) { + if (CollectionUtils.isEmpty(configurers)) { return; } - if (configurers.size() > 1) { - throw new IllegalStateException("only one TransactionManagementConfigurer may exist"); + throw new IllegalStateException("Only one TransactionManagementConfigurer may exist"); } - TransactionManagementConfigurer configurer = configurers.iterator().next(); this.txManager = configurer.annotationDrivenTransactionManager(); } diff --git a/spring-tx/src/main/java/org/springframework/transaction/annotation/TransactionManagementConfigurationSelector.java b/spring-tx/src/main/java/org/springframework/transaction/annotation/TransactionManagementConfigurationSelector.java index a12be89d71d..b82c96e0668 100644 --- a/spring-tx/src/main/java/org/springframework/transaction/annotation/TransactionManagementConfigurationSelector.java +++ b/spring-tx/src/main/java/org/springframework/transaction/annotation/TransactionManagementConfigurationSelector.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2013 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. @@ -45,13 +45,12 @@ public class TransactionManagementConfigurationSelector protected String[] selectImports(AdviceMode adviceMode) { switch (adviceMode) { case PROXY: - return new String[] { AutoProxyRegistrar.class.getName(), ProxyTransactionManagementConfiguration.class.getName() }; + return new String[] {AutoProxyRegistrar.class.getName(), ProxyTransactionManagementConfiguration.class.getName()}; case ASPECTJ: - return new String[] { TransactionManagementConfigUtils.TRANSACTION_ASPECT_CONFIGURATION_CLASS_NAME }; + return new String[] {TransactionManagementConfigUtils.TRANSACTION_ASPECT_CONFIGURATION_CLASS_NAME}; default: return null; } } - }