views:

34

answers:

3

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>
A: 

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>.

skaffman
A: 

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.

mezmo
No, it will not create multiple contexts, it will just create multiple beans.
skaffman
+1  A: 

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.

Espen