Reputation: 8582
If I use component scanning in Spring 2.5 but then also define a controller in xml.
Do I get two instances of this bean in my application context? If so which instance will be called for its related RequestMappings?
<bean id="myController" class="domain.MyController">
<property name="filters">
<list>
<ref local="filter1"/>
<ref local="filter2"/>
</list>
</property>
</bean>
Upvotes: 4
Views: 1772
Reputation: 10745
If you're asking Spring for a bean of a given interface and you have two beans of that interface, then you get an exception from the Spring container.
An exception from this rule is if your component is marked with @Primary
or the XML bean has the primary attribute set to true.
Upvotes: 1
Reputation: 2480
I've done that accidentally before, and it usually results in multiple application contexts. While everything LOOKED like it was working fine, little things like database changes never being committed were usually how I had to track it down.
Upvotes: 0
Reputation: 403481
Good question, I'm not sure. My guess is that whichever one is declared first, wins. So if your <context:component-scan>
comes first, the auto-detected component will get the request. If your <bean>
comes first, then that wins.
If in doubt, test it, it shouldn't be hard to find out.
A better solution would be to explicitly exclude the component from the scanner, using the nested filter elements of <context:component-scan>
.
Upvotes: 0