Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Autowiring modes in spring

    • 0
    • 1
    • 1
    • 2
    • 0
    • 0
    • 0
    • 0
    • 528
    Comment on it

    Autowiring modes:-Autowiring modes in spring are of 5 types:
    Default, byName, byType, byConstructor and autodetect


    Address.java

        package com.chandan;
    
    public class Address {
    
        private String city;
    
        public Address()
        {
    
        }
        public String getCity() {
            return city;
        }
    
        public void setCity(String city) {
            this.city = city;
        }
    
    
    }
    

    Teacher.java

        package com.chandan;
    
    public class Teacher {
        private String name;
        private int id;
        private Address address;
    
    
    
    
        public Address getAddress() {
            return address;
        }
    
    
        public void setAddress(Address address) {
            this.address = address;
        }
    
    
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
    
    
    
    }
    

    the spring can have 5 modes:-

    Default :- This is default mode means the no autowiring is enabled we can explicitly mention the ref of an particular bean.

    <?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:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
       <context:annotation-config/>
    
        <bean id="add" class="com.chandan.Address">
            <property name="city" value="Dehradun"/>
        </bean>
    
        <bean id="teacher" class="com.chandan.Teacher">
            <property name="id" value="1001"></property>
            <property name="name" value="Chandan Ahluwalia"></property>
            <property name="address" ref="add"></property>
        </bean>
    
    
    </beans>
    

    byName:-the name of a bean is same as the name of other bean property.the container can check that the match and wire its properties with the beans defined by the same names in the configuration file.

    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
       <context:annotation-config/>
    
        <bean id="address" class="com.chandan.Address">
            <property name="city" value="Dehradun"/>
        </bean>
    
        <bean id="teacher" class="com.chandan.Teacher" autowire="byName">
            <property name="id" value="1001"></property>
            <property name="name" value="Chandan Ahluwalia"></property>
    
        </bean>
    
    
    </beans>
    

    byType:- In this mode the data type of a bean is compatible with the data type of other bean property.in case if no bean with the property type is found, the property injection is ignored.

        <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
       <context:annotation-config/>
    
        <bean id="add" class="com.chandan.Address">
            <property name="city" value="Dehradun"/>
        </bean>
    
        <bean id="teacher" class="com.chandan.Teacher" autowire="byType">
            <property name="id" value="1001"></property>
            <property name="name" value="Chandan Ahluwalia"></property>
    
        </bean>
    
    
    </beans>
    

    byConstructor:- This is same as the byType.the data type of a bean is same as the data type of other bean constructor argument.in case if no bean with the property type is found, the property injection is ignored.


    Teacher.java

        package com.chandan;
    
    public class Teacher {
        private String name;
        private int id;
        private Address address;
    
        public Teacher(Address address)
        {
            this.address=address;
        }
    
    
        public Address getAddress() {
            return address;
        }
    
    
        public void setAddress(Address address) {
            this.address = address;
        }
    
    
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
    
    
    
    }
    
    <?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:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
       <context:annotation-config/>
    
        <bean id="address" class="com.chandan.Address">
            <property name="city" value="Dehradun"/>
        </bean>
    
        <bean id="teacher" class="com.chandan.Teacher" autowire="constructor">
            <property name="id" value="1001"></property>
            <property name="name" value="Chandan Ahluwalia"></property>
    
        </bean>
    
    
    </beans>
    

    autodetect:- The spring can first try to autowire by using constructor autowiring, if it does not work then spring try to autowire by "byType".

        <?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:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
       <context:annotation-config/>
    
        <bean id="address" class="com.chandan.Address">
            <property name="city" value="Dehradun"/>
        </bean>
    
        <bean id="teacher" class="com.chandan.Teacher" autowire=autodetect>
            <property name="id" value="1001"></property>
            <property name="name" value="Chandan Ahluwalia"></property>
    
        </bean>
    
    
    </beans>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: