From 91df0653fe5e22477ec2ed6dbe7e44b835c99038 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 17 Apr 2017 15:05:34 +0200 Subject: [PATCH] Polishing --- ...onfigurationClassBeanDefinitionReader.java | 6 ++- .../annotation/ConfigurationClassParser.java | 37 ++++++++++++------- .../ConfigurationClassPostProcessor.java | 32 ++++++++-------- .../aop/framework/CglibProxyTests.java | 6 +-- .../src/test/resources/log4j2-test.xml | 3 ++ .../org/springframework/util/ClassUtils.java | 24 ++++++------ 6 files changed, 61 insertions(+), 47 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassBeanDefinitionReader.java b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassBeanDefinitionReader.java index 4b7befd016..c74988d705 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassBeanDefinitionReader.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassBeanDefinitionReader.java @@ -247,7 +247,8 @@ class ConfigurationClassBeanDefinitionReader { BeanDefinition beanDefToRegister = beanDef; if (proxyMode != ScopedProxyMode.NO) { BeanDefinitionHolder proxyDef = ScopedProxyCreator.createScopedProxy( - new BeanDefinitionHolder(beanDef, beanName), this.registry, proxyMode == ScopedProxyMode.TARGET_CLASS); + new BeanDefinitionHolder(beanDef, beanName), this.registry, + proxyMode == ScopedProxyMode.TARGET_CLASS); beanDefToRegister = new ConfigurationClassBeanDefinition( (RootBeanDefinition) proxyDef.getBeanDefinition(), configClass, metadata); } @@ -272,7 +273,8 @@ class ConfigurationClassBeanDefinitionReader { // preserve the existing bean definition. if (existingBeanDef instanceof ConfigurationClassBeanDefinition) { ConfigurationClassBeanDefinition ccbd = (ConfigurationClassBeanDefinition) existingBeanDef; - return (ccbd.getMetadata().getClassName().equals(beanMethod.getConfigurationClass().getMetadata().getClassName())); + return ccbd.getMetadata().getClassName().equals( + beanMethod.getConfigurationClass().getMetadata().getClassName()); } // A bean definition resulting from a component scan can be silently overridden diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java index 1985f419a2..165a801be1 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java @@ -232,7 +232,7 @@ class ConfigurationClassParser { // Explicit bean definition found, probably replacing an import. // Let's remove the old one and go with the new one. this.configurationClasses.remove(configClass); - for (Iterator it = this.knownSuperclasses.values().iterator(); it.hasNext(); ) { + for (Iterator it = this.knownSuperclasses.values().iterator(); it.hasNext();) { if (configClass.equals(it.next())) { it.remove(); } @@ -258,13 +258,16 @@ class ConfigurationClassParser { * @param sourceClass a source class * @return the superclass, or {@code null} if none found or previously processed */ - protected final SourceClass doProcessConfigurationClass(ConfigurationClass configClass, SourceClass sourceClass) throws IOException { + protected final SourceClass doProcessConfigurationClass(ConfigurationClass configClass, SourceClass sourceClass) + throws IOException { + // Recursively process any member (nested) classes first processMemberClasses(configClass, sourceClass); // Process any @PropertySource annotations for (AnnotationAttributes propertySource : AnnotationConfigUtils.attributesForRepeatable( - sourceClass.getMetadata(), PropertySources.class, org.springframework.context.annotation.PropertySource.class)) { + sourceClass.getMetadata(), PropertySources.class, + org.springframework.context.annotation.PropertySource.class)) { if (this.environment instanceof ConfigurableEnvironment) { processPropertySource(propertySource); } @@ -277,14 +280,16 @@ class ConfigurationClassParser { // Process any @ComponentScan annotations Set componentScans = AnnotationConfigUtils.attributesForRepeatable( sourceClass.getMetadata(), ComponentScans.class, ComponentScan.class); - if (!componentScans.isEmpty() && !this.conditionEvaluator.shouldSkip(sourceClass.getMetadata(), ConfigurationPhase.REGISTER_BEAN)) { + if (!componentScans.isEmpty() && + !this.conditionEvaluator.shouldSkip(sourceClass.getMetadata(), ConfigurationPhase.REGISTER_BEAN)) { for (AnnotationAttributes componentScan : componentScans) { // The config class is annotated with @ComponentScan -> perform the scan immediately Set scannedBeanDefinitions = this.componentScanParser.parse(componentScan, sourceClass.getMetadata().getClassName()); - // Check the set of scanned definitions for any further config classes and parse recursively if necessary + // Check the set of scanned definitions for any further config classes and parse recursively if needed for (BeanDefinitionHolder holder : scannedBeanDefinitions) { - if (ConfigurationClassUtils.checkConfigurationClassCandidate(holder.getBeanDefinition(), this.metadataReaderFactory)) { + if (ConfigurationClassUtils.checkConfigurationClassCandidate( + holder.getBeanDefinition(), this.metadataReaderFactory)) { parse(holder.getBeanDefinition().getBeanClassName(), holder.getBeanName()); } } @@ -514,7 +519,9 @@ class ConfigurationClassParser { * @param visited used to track visited classes to prevent infinite recursion * @throws IOException if there is any problem reading metadata from the named class */ - private void collectImports(SourceClass sourceClass, Set imports, Set visited) throws IOException { + private void collectImports(SourceClass sourceClass, Set imports, Set visited) + throws IOException { + if (visited.add(sourceClass)) { for (SourceClass annotation : sourceClass.getAnnotations()) { String annName = annotation.getMetadata().getClassName(); @@ -541,7 +548,8 @@ class ConfigurationClassParser { throw ex; } catch (Throwable ex) { - throw new BeanDefinitionStoreException("Failed to process import candidates for configuration class [" + + throw new BeanDefinitionStoreException( + "Failed to process import candidates for configuration class [" + configClass.getMetadata().getClassName() + "]", ex); } } @@ -600,7 +608,8 @@ class ConfigurationClassParser { throw ex; } catch (Throwable ex) { - throw new BeanDefinitionStoreException("Failed to process import candidates for configuration class [" + + throw new BeanDefinitionStoreException( + "Failed to process import candidates for configuration class [" + configClass.getMetadata().getClassName() + "]", ex); } finally { @@ -739,9 +748,9 @@ class ConfigurationClassParser { private final DeferredImportSelector importSelector; - public DeferredImportSelectorHolder(ConfigurationClass configurationClass, DeferredImportSelector importSelector) { - this.configurationClass = configurationClass; - this.importSelector = importSelector; + public DeferredImportSelectorHolder(ConfigurationClass configClass, DeferredImportSelector selector) { + this.configurationClass = configClass; + this.importSelector = selector; } public ConfigurationClass getConfigurationClass() { @@ -881,8 +890,8 @@ class ConfigurationClassParser { return result; } - public Collection getAnnotationAttributes(String annotationType, String attribute) throws IOException { - Map annotationAttributes = this.metadata.getAnnotationAttributes(annotationType, true); + public Collection getAnnotationAttributes(String annType, String attribute) throws IOException { + Map annotationAttributes = this.metadata.getAnnotationAttributes(annType, true); if (annotationAttributes == null || !annotationAttributes.containsKey(attribute)) { return Collections.emptySet(); } diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java index e5bab7aaeb..45fbd2f76d 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 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. @@ -287,11 +287,11 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo }); // Detect any custom bean name generation strategy supplied through the enclosing application context - SingletonBeanRegistry singletonRegistry = null; + SingletonBeanRegistry sbr = null; if (registry instanceof SingletonBeanRegistry) { - singletonRegistry = (SingletonBeanRegistry) registry; - if (!this.localBeanNameGeneratorSet && singletonRegistry.containsSingleton(CONFIGURATION_BEAN_NAME_GENERATOR)) { - BeanNameGenerator generator = (BeanNameGenerator) singletonRegistry.getSingleton(CONFIGURATION_BEAN_NAME_GENERATOR); + sbr = (SingletonBeanRegistry) registry; + if (!this.localBeanNameGeneratorSet && sbr.containsSingleton(CONFIGURATION_BEAN_NAME_GENERATOR)) { + BeanNameGenerator generator = (BeanNameGenerator) sbr.getSingleton(CONFIGURATION_BEAN_NAME_GENERATOR); this.componentScanBeanNameGenerator = generator; this.importBeanNameGenerator = generator; } @@ -330,10 +330,10 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo } for (String candidateName : newCandidateNames) { if (!oldCandidateNames.contains(candidateName)) { - BeanDefinition beanDef = registry.getBeanDefinition(candidateName); - if (ConfigurationClassUtils.checkConfigurationClassCandidate(beanDef, this.metadataReaderFactory) && - !alreadyParsedClasses.contains(beanDef.getBeanClassName())) { - candidates.add(new BeanDefinitionHolder(beanDef, candidateName)); + BeanDefinition bd = registry.getBeanDefinition(candidateName); + if (ConfigurationClassUtils.checkConfigurationClassCandidate(bd, this.metadataReaderFactory) && + !alreadyParsedClasses.contains(bd.getBeanClassName())) { + candidates.add(new BeanDefinitionHolder(bd, candidateName)); } } } @@ -343,9 +343,9 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo while (!candidates.isEmpty()); // Register the ImportRegistry as a bean in order to support ImportAware @Configuration classes - if (singletonRegistry != null) { - if (!singletonRegistry.containsSingleton(IMPORT_REGISTRY_BEAN_NAME)) { - singletonRegistry.registerSingleton(IMPORT_REGISTRY_BEAN_NAME, parser.getImportRegistry()); + if (sbr != null) { + if (!sbr.containsSingleton(IMPORT_REGISTRY_BEAN_NAME)) { + sbr.registerSingleton(IMPORT_REGISTRY_BEAN_NAME, parser.getImportRegistry()); } } @@ -417,7 +417,9 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo } @Override - public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) { + public PropertyValues postProcessPropertyValues( + PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) { + // Inject the BeanFactory before AutowiredAnnotationBeanPostProcessor's // postProcessPropertyValues method attempts to autowire other configuration beans. if (bean instanceof EnhancedConfiguration) { @@ -429,8 +431,8 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo @Override public Object postProcessBeforeInitialization(Object bean, String beanName) { if (bean instanceof ImportAware) { - ImportRegistry importRegistry = this.beanFactory.getBean(IMPORT_REGISTRY_BEAN_NAME, ImportRegistry.class); - AnnotationMetadata importingClass = importRegistry.getImportingClassFor(bean.getClass().getSuperclass().getName()); + ImportRegistry ir = this.beanFactory.getBean(IMPORT_REGISTRY_BEAN_NAME, ImportRegistry.class); + AnnotationMetadata importingClass = ir.getImportingClassFor(bean.getClass().getSuperclass().getName()); if (importingClass != null) { ((ImportAware) bean).setImportMetadata(importingClass); } diff --git a/spring-context/src/test/java/org/springframework/aop/framework/CglibProxyTests.java b/spring-context/src/test/java/org/springframework/aop/framework/CglibProxyTests.java index 8d997271bd..f887ee70bb 100644 --- a/spring-context/src/test/java/org/springframework/aop/framework/CglibProxyTests.java +++ b/spring-context/src/test/java/org/springframework/aop/framework/CglibProxyTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 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. @@ -270,9 +270,7 @@ public class CglibProxyTests extends AbstractAopProxyTests implements Serializab aop.setConstructorArguments(new Object[] {"Rob Harrop", 22}, new Class[] {String.class, int.class}); NoArgCtorTestBean proxy = (NoArgCtorTestBean) aop.getProxy(); - proxy = (NoArgCtorTestBean) aop.getProxy(); - - assertNotNull("Proxy should be null", proxy); + assertNotNull(proxy); } @Test diff --git a/spring-context/src/test/resources/log4j2-test.xml b/spring-context/src/test/resources/log4j2-test.xml index 04c4fbc44c..29784b2f51 100644 --- a/spring-context/src/test/resources/log4j2-test.xml +++ b/spring-context/src/test/resources/log4j2-test.xml @@ -6,6 +6,9 @@ + + + diff --git a/spring-core/src/main/java/org/springframework/util/ClassUtils.java b/spring-core/src/main/java/org/springframework/util/ClassUtils.java index 3f8eedc60e..5683abfa76 100644 --- a/spring-core/src/main/java/org/springframework/util/ClassUtils.java +++ b/spring-core/src/main/java/org/springframework/util/ClassUtils.java @@ -1053,10 +1053,10 @@ public abstract class ClassUtils { } /** - * Return all interfaces that the given instance implements as array, + * Return all interfaces that the given instance implements as an array, * including ones implemented by superclasses. * @param instance the instance to analyze for interfaces - * @return all interfaces that the given instance implements as array + * @return all interfaces that the given instance implements as an array */ public static Class[] getAllInterfaces(Object instance) { Assert.notNull(instance, "Instance must not be null"); @@ -1064,24 +1064,24 @@ public abstract class ClassUtils { } /** - * Return all interfaces that the given class implements as array, + * Return all interfaces that the given class implements as an array, * including ones implemented by superclasses. *

If the class itself is an interface, it gets returned as sole interface. * @param clazz the class to analyze for interfaces - * @return all interfaces that the given object implements as array + * @return all interfaces that the given object implements as an array */ public static Class[] getAllInterfacesForClass(Class clazz) { return getAllInterfacesForClass(clazz, null); } /** - * Return all interfaces that the given class implements as array, + * Return all interfaces that the given class implements as an array, * including ones implemented by superclasses. *

If the class itself is an interface, it gets returned as sole interface. * @param clazz the class to analyze for interfaces * @param classLoader the ClassLoader that the interfaces need to be visible in * (may be {@code null} when accepting all declared interfaces) - * @return all interfaces that the given object implements as array + * @return all interfaces that the given object implements as an array */ public static Class[] getAllInterfacesForClass(Class clazz, ClassLoader classLoader) { Set> ifcs = getAllInterfacesForClassAsSet(clazz, classLoader); @@ -1089,10 +1089,10 @@ public abstract class ClassUtils { } /** - * Return all interfaces that the given instance implements as Set, + * Return all interfaces that the given instance implements as a Set, * including ones implemented by superclasses. * @param instance the instance to analyze for interfaces - * @return all interfaces that the given instance implements as Set + * @return all interfaces that the given instance implements as a Set */ public static Set> getAllInterfacesAsSet(Object instance) { Assert.notNull(instance, "Instance must not be null"); @@ -1100,24 +1100,24 @@ public abstract class ClassUtils { } /** - * Return all interfaces that the given class implements as Set, + * Return all interfaces that the given class implements as a Set, * including ones implemented by superclasses. *

If the class itself is an interface, it gets returned as sole interface. * @param clazz the class to analyze for interfaces - * @return all interfaces that the given object implements as Set + * @return all interfaces that the given object implements as a Set */ public static Set> getAllInterfacesForClassAsSet(Class clazz) { return getAllInterfacesForClassAsSet(clazz, null); } /** - * Return all interfaces that the given class implements as Set, + * Return all interfaces that the given class implements as a Set, * including ones implemented by superclasses. *

If the class itself is an interface, it gets returned as sole interface. * @param clazz the class to analyze for interfaces * @param classLoader the ClassLoader that the interfaces need to be visible in * (may be {@code null} when accepting all declared interfaces) - * @return all interfaces that the given object implements as Set + * @return all interfaces that the given object implements as a Set */ public static Set> getAllInterfacesForClassAsSet(Class clazz, ClassLoader classLoader) { Assert.notNull(clazz, "Class must not be null");