Autowiring conflict in spring core with the xml configuration
How can I fix this issue just using the configuration in application context?
You could use the qualifier
tag like below (see https://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/beans.html#beans-autowired-annotation-qualifiers)
<context:annotation-config/>
<beans>
<bean class="your_pkg_route.Vehicle">
<qualifier value="bike"/>
</bean>
</beans>
</context:annotation-config>
I found people talking about autowire attribute in the bean declaration and I need more explanation about it
Using Annotation
@Autowired
used on a bean declaration method injects the defined dependencies by (another) declared beans. Now, if your dependencies are in the same context of your application, you don't need to use the @Autowired
annotation at all because Spring is able to figure them out by itself. So, if your dependencies are outside your applicatoin context then you can use it.
For example, take as reference the below code:
@Autowired
@Bean
public MyBean getMybean(Dependency1 depdency1, Dependency2 depdency2) {
return new MyBean(depdency1.getSomeStuff(), depdency2.getSomeOtherStuff());
}
Here, @Autowired
will find an instance of Dependency1
and Dependency2
and will provide them for the creation of an instance of MyBean
.
Using xml configuration
From Pro Spring 5... Spring supports five modes for autowiring.
byName
: When usingbyName
autowiring, Spring attempts to wire each property to a bean of the same name. So, if the target bean has a property namedfoo
and afoo
bean is defined inApplicationContext
, thefoo
bean is assigned to thefoo
property of the target.byType
: When usingbyType
autowiring, Spring attempts to wire each of the properties on the target bean by automatically using a bean of the same type inApplicationContext
.constructor
: This functions just likebyType
wiring, except that it uses constructors rather than setters to perform the injection. Spring attempts to match the greatest numbers of arguments it can in the constructor. So, if your bean has two constructors, one that accepts aString
and one that acceptsString
and anInteger
, and you have both aString
and anInteger
bean in yourApplicationContext
, Spring uses the two-argument constructor.default
: Spring will choose between theconstructor
andbyType
modes automatically. If your bean has a default (no-arguments) constructor, Spring usesbyType
; otherwise, it uses constructor.no
: This is the default
So, in your case you would need to do something like this (BUT, I would NOT recommend it. Why?, you would need to declare Vehicle
class as a bean and a component which is not correct, see Spring: @Component versus @Bean. On the other hand I'm not sure if you could use it just declaring it as a bean):
// xml config
<context:annotation-config/>
<beans>
// use the primary tag here too! in order to say this the primary bean
// this only works when there are only two implementations of the same interface
<bean id="bike" primary="true" class="your_pkg_route.Bike"/>
<bean id="car" class="your_pkg_route.Car"/>
<bean autowire="byName" class="your_pkg_route.VehicleService"/>
<beans>
</context:annotation-config>
// VehicleService
@Component
public class VehicleService {
private Vehicle bike; // call attribute 'bike' so it is autowired by its name
public void service() {
//...
}
}
As you can see there is a lot of complications trying to do this using xml config, so I would recommend you to use the annotation option if possible.
Related posts:
- Why do I not need @Autowired on @Bean methods in a Spring configuration class?
- Difference between @Bean and @Autowired
PS: I have not tested any of the posted codes.
You can use @Primary instead of @Qualifier
@Primary
@Component(value="bike")
public class Bike implements Vehicle {
we use @Primary to give higher preference to a bean when there are multiple beans of the same type.
We can use @Primary directly on the beans
You can also set primary attribute in XML:
property has primary attribute:
<bean primary="true|false"/>
If a @Primary-annotated class is declared via XML, @Primary annotation metadata is ignored, and is respected instead.