Hi,
I was playing around with the new java-config features of Spring 3.1 and I stumbled across what is either a bug or a misunderstanding of the supported behavior.
Assume the following class definitions:
Code:package com.foo; import org.springframework.stereotype.Component; @Component public class ComponentA { }
Two classes, same simple name (same bean name), different packages. Let's also pretend that packages com.foo and com.bar also contain a bunch of different components. In a single context, I want the sum of all components defined in packages com.foo and com.bar, knowing that their would be a clash for ComponentA because it is defined in the two packages, so I tried the following:Code:package com.bar; import org.springframework.stereotype.Component; @Component public class ComponentA { }
Code:package com.potato; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.FilterType; @Configuration @ComponentScan(basePackages = {"com.foo", "com.bar"}, excludeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = com.foo.ComponentA.class)}) public class TestConfig { }
This throws the following exceptionCode:public class Main { public static void main(String[] args) { AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); rootContext.scan("com.potato"); rootContext.refresh(); rootContext.close(); } }
But, if I simply replace the usage of the "scan" method for the "register" method on the context, it works:Code:Exception in thread "main" org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'componentA' for bean class [com.bar.ComponentA] conflicts with existing, non-compatible bean definition of same name and class [com.foo.ComponentA]
The reason I am using the "scan" method is because the com.potato package contains other configuration classes that I want loaded, as opposed to registering them one by one. Is this supposed to work? I put a breakpoint in "parse" method of the org.springframework.context.annotation.ComponentSc anAnnotationParser class where it seems to register the exclude filters, and when I register my config classes using the "scan" method of the AnnotationConfigWebApplicationContext, it never stops there (it does when I use the "register" method though). I know the work around is simply to register my config classes one by one, but it would be nice that it worked both ways.Code:public class Main { public static void main(String[] args) { AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); rootContext.register(com.potato.TestConfig.class); rootContext.refresh(); rootContext.close(); } }
Thank you.


Reply With Quote