Wednesday, September 9, 2015

How to deal with Ambiguities in Autowiring - Spring

In the last post - Autowiring in Spring we discussed Autowirirng and different methods of doing it. Also I gave an introduction to ambiguities in autowiring, so this tutorial is a continuation of it. 

Here I will discuss two other ways we can deal with the ambiguity in autowiring.

1. Autowiring Candidate property


 Autowiring candidate is a way to turn on or turn off autowiring of a bean and it is set per bean. Autowiring-candidate property has several choices : 
  • true - If it is set to true, then it can wire with the other bean.
  • false - If it is set to false, then it autowiring is totally prohibited.
  • default -  This is the default value set and can be considered to autowire.
Now, let's look at our sample code.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
    default-autowire="byType">

    <bean id="abcompany" class="com.kasibsblog.spring.test.AbCompany">
    </bean>

    <bean name="parttime" class="com.kasibsblog.spring.test.PartTimeEmployee">
    </bean>
   
    <bean name="parttime2"
        class="com.kasibsblog.spring.test.PartTimeEmployee"
        autowire-candidate="false">
    </bean>

    <bean name="fulltime"
        class="com.kasibsblog.spring.test.FullTimeEmployee">

</bean>
</beans>


The bean with id - parttime2, cannot be autowired and it is prohibited. If the autowire-candidate property was set to either default or true, the program will throw an exception as the autowiring method is set to byType.



2. Primary property

 

Primary property is another technique to overcome ambiguity in autowiring. We can set the primary property to either true or false. Unlike the autowiring-candidate property set to false, primary does not totally prohibit it from autowiring only the preference level is set by it.

Let me now explain this by an example,

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
    default-autowire="byType">

    <bean id="abcompany" class="com.kasibsblog.spring.test.AbCompany">
    </bean>

    <bean name="parttime" class="com.kasibsblog.spring.test.PartTimeEmployee">
    </bean>
   
    <bean name="parttime2"
        class="com.kasibsblog.spring.test.PartTimeEmployee"
        autowire-candidate="false">
    </bean>

    <bean name="fulltime"
        class="com.kasibsblog.spring.test.FullTimeEmployee"

        primary="true">
</bean>

<bean name="fulltime2"
        class="com.kasibsblog.spring.test.FullTimeEmployee">

</bean>
</beans>

In the above sample code, we have two beans of type - FullTimeEmploye, I had set the bean - fulltime to be my primary bean to autowire. This does not mean that fulltime2 is totally prohibited from autowiring. It can be autowired with other beans.

 

No comments :

Post a Comment