Browse Source
Prior to this change, a @Configuration classes that @ComponentScan themselves would result in a ConflictingBeanDefinitionException. For example: package com.foo.config; @Configuration @ComponentScan("com.foo"); public class AppConfig { // ... } This resulted in a ConflictingBeanDefinitionException that users have typically worked around in the following fashion: package com.foo.config; @Configuration @ComponentScan(basePackages="com.foo", excludeFilters=@Filter(value=ANNOTATION_TYPE, type=Configuration.class); public class AppConfig { // ... } This is obviously more verbose and cumbersome than would be desirable, and furthermore potentially too constraining as it prohibits the ability to include other legitimate @Configuration classes via scanning. The exception was being thrown because of a logic problem in ClassPathBeanDefinitionScanner. The bean definition for AppConfig gets registered once by the user (e.g. when constructing an AnnotationConfigApplicationContext), then again when performing the component scan for 'com.foo'. Prior to this change, ClassPathBeanDefinitionScanner's #isCompatible returned false if the new bean definition was anything other than an AnnotatedBeanDefinition. The intention of this check is really to see whether the new bean definition is a *scanned* bean definition, i.e. the result of a component-scanning operation. If so, then it becomes safe to assume that the original bean definition is the one that should be kept, as it is the one explicitly registered by the user. Therefore, the fix is as simple as narrowing the instanceof check from AnnotatedBeanDefinition to its ScannedGenericBeanDefinition subtype. Note that this commit partially reverts changes introduced in SPR-8307 that explicitly caught ConflictingBeanDefinitionExceptions when processing recursive @ComponentScan definitions, and rethrew as a "CircularComponentScanException. With the changes in this commit, such CBDEs will no longer occur, obviating the need for this check and for this custom exception type altogether. Issue: SPR-8808, SPR-8307pull/7/head
Chris Beams
13 years ago
5 changed files with 56 additions and 44 deletions
@ -1,32 +0,0 @@ |
|||||||
/* |
|
||||||
* 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,49 @@ |
|||||||
|
/* |
||||||
|
* 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.spr8808; |
||||||
|
|
||||||
|
import org.junit.Test; |
||||||
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
||||||
|
import org.springframework.context.annotation.ComponentScan; |
||||||
|
import org.springframework.context.annotation.Configuration; |
||||||
|
|
||||||
|
/** |
||||||
|
* Tests cornering the bug in which @Configuration classes that @ComponentScan themselves |
||||||
|
* would result in a ConflictingBeanDefinitionException. |
||||||
|
* |
||||||
|
* @author Chris Beams |
||||||
|
* @since 3.1 |
||||||
|
*/ |
||||||
|
public class Spr8808Tests { |
||||||
|
|
||||||
|
/** |
||||||
|
* This test failed with ConflictingBeanDefinitionException prior to fixes for |
||||||
|
* SPR-8808. |
||||||
|
*/ |
||||||
|
@Test |
||||||
|
public void repro() { |
||||||
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); |
||||||
|
ctx.register(Config.class); |
||||||
|
ctx.refresh(); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Configuration |
||||||
|
@ComponentScan(basePackageClasses=Spr8808Tests.class) // scan *this* package
|
||||||
|
class Config { |
||||||
|
} |
Loading…
Reference in new issue