Browse Source
Prior to this change, @ComponentScan annotations were only processed at the first level of depth. Now, the set of bean definitions resulting from each declaration of @ComponentScan is checked for configuration classes that declare @ComponentScan, and recursion is performed as necessary. Cycles between @ComponentScan declarations are detected as well. See CircularComponentScanException. Issue: SPR-8307 git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@4275 50f2f4bb-b051-0410-bef5-90022cba6387pull/1/merge
15 changed files with 429 additions and 108 deletions
@ -0,0 +1,32 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2002-2011 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. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
|
||||||
|
package org.springframework.context.annotation; |
||||||
|
|
||||||
|
/** |
||||||
|
* Exception thrown upon detection of circular {@link ComponentScan} use. |
||||||
|
* |
||||||
|
* @author Chris Beams |
||||||
|
* @since 3.1 |
||||||
|
*/ |
||||||
|
@SuppressWarnings("serial") |
||||||
|
class CircularComponentScanException extends IllegalStateException { |
||||||
|
|
||||||
|
public CircularComponentScanException(String message, Exception cause) { |
||||||
|
super(message, cause); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,102 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2002-2011 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. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
|
||||||
|
package org.springframework.context.annotation; |
||||||
|
|
||||||
|
import java.io.IOException; |
||||||
|
|
||||||
|
import org.apache.commons.logging.Log; |
||||||
|
import org.apache.commons.logging.LogFactory; |
||||||
|
import org.springframework.beans.factory.config.BeanDefinition; |
||||||
|
import org.springframework.beans.factory.support.AbstractBeanDefinition; |
||||||
|
import org.springframework.core.Conventions; |
||||||
|
import org.springframework.core.type.AnnotationMetadata; |
||||||
|
import org.springframework.core.type.StandardAnnotationMetadata; |
||||||
|
import org.springframework.core.type.classreading.MetadataReader; |
||||||
|
import org.springframework.core.type.classreading.MetadataReaderFactory; |
||||||
|
import org.springframework.stereotype.Component; |
||||||
|
|
||||||
|
/** |
||||||
|
* Utilities for processing @{@link Configuration} classes. |
||||||
|
* |
||||||
|
* @author Chris Beams |
||||||
|
* @since 3.1 |
||||||
|
*/ |
||||||
|
abstract class ConfigurationClassUtils { |
||||||
|
|
||||||
|
private static final Log logger = LogFactory.getLog(ConfigurationClassUtils.class); |
||||||
|
|
||||||
|
private static final String CONFIGURATION_CLASS_FULL = "full"; |
||||||
|
|
||||||
|
private static final String CONFIGURATION_CLASS_LITE = "lite"; |
||||||
|
|
||||||
|
private static final String CONFIGURATION_CLASS_ATTRIBUTE = |
||||||
|
Conventions.getQualifiedAttributeName(ConfigurationClassPostProcessor.class, "configurationClass"); |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Check whether the given bean definition is a candidate for a configuration class, |
||||||
|
* and mark it accordingly. |
||||||
|
* @param beanDef the bean definition to check |
||||||
|
* @param metadataReaderFactory the current factory in use by the caller |
||||||
|
* @return whether the candidate qualifies as (any kind of) configuration class
|
||||||
|
*/ |
||||||
|
public static boolean checkConfigurationClassCandidate(BeanDefinition beanDef, MetadataReaderFactory metadataReaderFactory) { |
||||||
|
AnnotationMetadata metadata = null; |
||||||
|
|
||||||
|
// Check already loaded Class if present...
|
||||||
|
// since we possibly can't even load the class file for this Class.
|
||||||
|
if (beanDef instanceof AbstractBeanDefinition && ((AbstractBeanDefinition) beanDef).hasBeanClass()) { |
||||||
|
metadata = new StandardAnnotationMetadata(((AbstractBeanDefinition) beanDef).getBeanClass()); |
||||||
|
} |
||||||
|
else { |
||||||
|
String className = beanDef.getBeanClassName(); |
||||||
|
if (className != null) { |
||||||
|
try { |
||||||
|
MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(className); |
||||||
|
metadata = metadataReader.getAnnotationMetadata(); |
||||||
|
} |
||||||
|
catch (IOException ex) { |
||||||
|
if (logger.isDebugEnabled()) { |
||||||
|
logger.debug("Could not find class file for introspecting factory methods: " + className, ex); |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
if (metadata != null) { |
||||||
|
if (metadata.isAnnotated(Configuration.class.getName())) { |
||||||
|
beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_FULL); |
||||||
|
return true; |
||||||
|
} |
||||||
|
else if (metadata.isAnnotated(Component.class.getName()) || |
||||||
|
metadata.hasAnnotatedMethods(Bean.class.getName())) { |
||||||
|
beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_LITE); |
||||||
|
return true; |
||||||
|
} |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Determine whether the given bean definition indicates a full @Configuration class. |
||||||
|
*/ |
||||||
|
public static boolean isFullConfigurationClass(BeanDefinition beanDef) { |
||||||
|
return CONFIGURATION_CLASS_FULL.equals(beanDef.getAttribute(CONFIGURATION_CLASS_ATTRIBUTE)); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,33 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2002-2011 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. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
|
||||||
|
package org.springframework.context.annotation; |
||||||
|
|
||||||
|
/** |
||||||
|
* Marker subclass of {@link IllegalStateException}, allowing for explicit |
||||||
|
* catch clauses in calling code. |
||||||
|
* |
||||||
|
* @author Chris Beams |
||||||
|
* @since 3.1 |
||||||
|
*/ |
||||||
|
@SuppressWarnings("serial") |
||||||
|
class ConflictingBeanDefinitionException extends IllegalStateException { |
||||||
|
|
||||||
|
public ConflictingBeanDefinitionException(String message) { |
||||||
|
super(message); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,60 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2002-2011 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. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
|
||||||
|
package org.springframework.context.annotation; |
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.sameInstance; |
||||||
|
import static org.junit.Assert.assertThat; |
||||||
|
|
||||||
|
import org.junit.Test; |
||||||
|
import org.springframework.context.annotation.componentscan.cycle.left.LeftConfig; |
||||||
|
import org.springframework.context.annotation.componentscan.level1.Level1Config; |
||||||
|
import org.springframework.context.annotation.componentscan.level2.Level2Config; |
||||||
|
import org.springframework.context.annotation.componentscan.level3.Level3Component; |
||||||
|
|
||||||
|
/** |
||||||
|
* Tests ensuring that configuration clasess marked with @ComponentScan |
||||||
|
* may be processed recursively |
||||||
|
* |
||||||
|
* @author Chris Beams |
||||||
|
* @since 3.1 |
||||||
|
*/ |
||||||
|
public class ComponentScanAnnotationRecursionTests { |
||||||
|
|
||||||
|
@Test |
||||||
|
public void recursion() { |
||||||
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); |
||||||
|
ctx.register(Level1Config.class); |
||||||
|
ctx.refresh(); |
||||||
|
|
||||||
|
// assert that all levels have been detected
|
||||||
|
ctx.getBean(Level1Config.class); |
||||||
|
ctx.getBean(Level2Config.class); |
||||||
|
ctx.getBean(Level3Component.class); |
||||||
|
|
||||||
|
// assert that enhancement is working
|
||||||
|
assertThat(ctx.getBean("level1Bean"), sameInstance(ctx.getBean("level1Bean"))); |
||||||
|
assertThat(ctx.getBean("level2Bean"), sameInstance(ctx.getBean("level2Bean"))); |
||||||
|
} |
||||||
|
|
||||||
|
@Test(expected=CircularComponentScanException.class) |
||||||
|
public void cycleDetection() { |
||||||
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); |
||||||
|
ctx.register(LeftConfig.class); |
||||||
|
ctx.refresh(); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,26 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2002-2011 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. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
|
||||||
|
package org.springframework.context.annotation.componentscan.cycle.left; |
||||||
|
|
||||||
|
import org.springframework.context.annotation.ComponentScan; |
||||||
|
import org.springframework.context.annotation.Configuration; |
||||||
|
|
||||||
|
@Configuration |
||||||
|
@ComponentScan("org.springframework.context.annotation.componentscan.cycle.right") |
||||||
|
public class LeftConfig { |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,26 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2002-2011 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. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
|
||||||
|
package org.springframework.context.annotation.componentscan.cycle.right; |
||||||
|
|
||||||
|
import org.springframework.context.annotation.ComponentScan; |
||||||
|
import org.springframework.context.annotation.Configuration; |
||||||
|
|
||||||
|
@Configuration |
||||||
|
@ComponentScan("org.springframework.context.annotation.componentscan.cycle.left") |
||||||
|
public class RightConfig { |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,31 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2002-2011 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. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
|
||||||
|
package org.springframework.context.annotation.componentscan.level1; |
||||||
|
|
||||||
|
import org.springframework.beans.TestBean; |
||||||
|
import org.springframework.context.annotation.Bean; |
||||||
|
import org.springframework.context.annotation.ComponentScan; |
||||||
|
import org.springframework.context.annotation.Configuration; |
||||||
|
|
||||||
|
@Configuration |
||||||
|
@ComponentScan("org.springframework.context.annotation.componentscan.level2") |
||||||
|
public class Level1Config { |
||||||
|
@Bean |
||||||
|
public TestBean level1Bean() { |
||||||
|
return new TestBean("level1Bean"); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,31 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2002-2011 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. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
|
||||||
|
package org.springframework.context.annotation.componentscan.level2; |
||||||
|
|
||||||
|
import org.springframework.beans.TestBean; |
||||||
|
import org.springframework.context.annotation.Bean; |
||||||
|
import org.springframework.context.annotation.ComponentScan; |
||||||
|
import org.springframework.context.annotation.Configuration; |
||||||
|
|
||||||
|
@Configuration |
||||||
|
@ComponentScan("org.springframework.context.annotation.componentscan.level3") |
||||||
|
public class Level2Config { |
||||||
|
@Bean |
||||||
|
public TestBean level2Bean() { |
||||||
|
return new TestBean("level2Bean"); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,24 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2002-2011 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. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
|
||||||
|
package org.springframework.context.annotation.componentscan.level3; |
||||||
|
|
||||||
|
import org.springframework.stereotype.Component; |
||||||
|
|
||||||
|
@Component |
||||||
|
public class Level3Component { |
||||||
|
|
||||||
|
} |
||||||
Loading…
Reference in new issue