Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to exclude a bean from auto-wiring?

    • 0
    • 1
    • 0
    • 2
    • 0
    • 0
    • 0
    • 0
    • 1.46k
    Comment on it

    When configuring beans in configuration file, set the "autowire-candidate" attribute of the bean element in .xml file to false, by doing this the container will exclude that specific bean from auto-wiring.

    By using the below example we can easily show that how we can exclude the Department bean from Employee bean the value of that Object is null.

    Ex:- Employee.java

    package com.babita;
    
        import org.springframework.beans.factory.annotation.Autowired;
        import javax.annotation.Resource;
    
        public class Employee {
    
        private int id;
        private String name;
        private Department department;
    
        public void setDepartment(Department department) {
            this.department = department;
        }
    
        public Department getDepartment() {
            return department;
        }
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    
        @Override
        public String toString(){
        return"Employee[id="
        +id+",Department="
        +department+",name="+name+"]";
        }
    }
    

    Department.java

    package com.babita;
    public class Department {
    
        private String name;
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        @Override
        public String toString() {
        return"Student[name="+name+"]";
        }
    
    }
    

    Now set the "autowire-candidate" to false in app-context.xml file:

    app-context.xml

    <?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="dep" class="com.babita.Department" >
               <property name="name" value="java"></property>
       </bean>
    
       <bean id="emp" class="com.babita.Employee" autowire-candidate="false">
               <property name="name" value="babita"></property>
               <property name="id" value="001"></property>
       </bean>
    
    
    </beans>
    

    Now Define the main class where you can see the output:

    App.java

    package com.app;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.babita.Department;
    import com.babita.Employee;
    import com.manish.Course;
    import com.manish.Student;
    
    public class App {
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            ApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"app-context.xml"});
            Employee emp=(Employee)ctx.getBean("emp");
            System.out.println(emp);
        }
    }
    

    Hope this will help you :)

 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: