Can I override spring beans using component scan?

If I use component checking in Spring 2.5, but then also define the controller in xml.

Can I get two instances of this bean in my application context? If so, which instance will be called for related related queries?

<bean id="myController" class="domain.MyController"> <property name="filters"> <list> <ref local="filter1"/> <ref local="filter2"/> </list> </property> </bean> 
+4
source share
3 answers

If you request Spring for the bean of this interface, and you have two beans of this interface, then you get an exception from the Spring container.

An exception to this rule is that your component is marked @Primary or the XML bean has a primary attribute set to true.

+1
source

Good question, I'm not sure. I assume that what is declared first wins. Therefore, if your <context:component-scan> first, the automatically detected component will receive a request. If your <bean> comes first, then it wins.

If in doubt, check it out, it should not be hard to find.

The best solution would be to explicitly exclude the component from the scanner using the nested filter elements <context:component-scan> .

0
source

I have done this before, and usually this leads to several application contexts. Although everything that LOOKS as if it is working fine, little has changed, since changes to the database have never been made, as a rule, I had to track this.

0
source

Source: https://habr.com/ru/post/1310383/


All Articles