There are 3 DI Mechanisms that autowiring follows :-
autowire="constructor" attribute in <bean> tag. @Autowired annotation on the constructor. autowire="byType" or autowire="byName" attribute in <bean> tag. @Autowired annotation on the setter method. @Autowired annotation directly on the field. Total there are 5 modes of autowiring in xml configuration which are listed as below :-
no (Default Mode)
<bean id="engine" class="com.example.Engine" />
<bean id="car" class="com.example.Car">
<property name="engine" ref="engine" />
</bean>
byName
<bean id="engine" class="com.example.Engine" />
<bean id="car" class="com.example.Car" autowire="byName" />
byType
<bean id="engine" class="com.example.Engine" />
<bean id="car" class="com.example.Car" autowire="byType" />
constructor
<bean id="engine" class="com.example.Engine" />
<bean id="car" class="com.example.Car" autowire="constructor" />
autodetect (Deprecated)
<bean id="engine" class="com.example.Engine" />
<bean id="car" class="com.example.Car" autowire="autodetect" />
autowire-candidate attribute in XML configuration ?
autowire-candidate is true.
autowire-candidate=false) when we have multiple beans of the same type and we want to prevent a specific bean from being injected automatically.
<bean id="userService" class="com.example.UserService" autowire="byType"/>
<bean id="userRepository" class="com.example.UserRepository" autowire-candidate="false"/>
In this case, userRepository will not be autowired into userService despite matching by type.
Some limitations or disadvantages of XML-based autowiring are as follows :-
@Autowired), which offer more flexibility.
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
Weβre here to make our tutorials better based on your thoughts and suggestions.