Autowire reference beans into list by type

后端 未结 2 564
花落未央
花落未央 2020-11-28 09:43

I have one class that has a list of objects of Daemon type.

class Xyz {    
    List daemons;
}

My spring config

2条回答
  •  渐次进展
    2020-11-28 10:11

    Well, this can be achieved in two ways as stated in Spring Documentation.

    Below is the excerpt from the documentation.

    With byType or constructor autowiring mode, you can wire arrays and typed collections.

    1. autowire="byType"

    Autowiring using "byType" can be achieved if the type of the bean defined in the xml matches the type of list.

    Example:

    Motor.java

    package com.chiranth;
    public interface Motor 
    {
       public void start();
    }
    

    ElectricMotor1.java

    package com.chiranth;
    public class ElectricMotor1 implements Motor
    {
         public void start() 
         { 
             System.out.println("Motor 1 Started.");
         }
    }
    

    ElectricMotor2.java

    package com.chiranth;
    public class ElectricMotor2 implements Motor
    {
        public void start() 
        {
            System.out.println("Motor 2 Started.");
        }
    }
    

    TeslaModelX.java

    package com.chiranth;
    import java.util.List;
    public class TeslaModelX 
    {
        private List motor;
    
        public List getMotor()
        {
            return motor;
        }
    
        public void setMotor(List motor) 
        {
            this.motor = motor;
        }
    
        public void goForward()
        {
            for(Motor m :motor)
                m.start();
            System.out.println("Going Forward.");
        }
    }
    

    Spring.xml

    
    
    
    
        
        
    
        
    
    

    Test.java

    package com.chiranth;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public class Test 
    {
        public static void main(String[] args) 
        {
            ApplicationContext context= new ClassPathXmlApplicationContext("Spring.xml");
            TeslaModelX modelx=(TeslaModelX)context.getBean("modelX");
            modelx.goForward();
        }
    }
    

    OUTPUT:

    Motor 1 Started.
    Motor 2 Started.
    Going Forward.
    

    2. autowire="constructor"

    Autowiring using "constructor" can be achieved if the type of the bean defined in the xml matches the type of the argument in the constructor.

    Example:

    Considering the above Motor.java , ElectricMotor1.java and ElectricMotor2.java.

    TeslaModelX.java

    package com.chiranth;
    import java.util.List;
    public class TeslaModelX 
    {
        private List motor;
    
        public TeslaModelX(List motor)
        {
            this.motor=motor;
        }
    
        public void goForward()
        {
            for(Motor m:motor)
                m.start();
            System.out.println("Going Forward.");
        }
    }
    

    Spring.xml

    
    
        
        
    
        
    
    

    Test.java

    package com.chiranth;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public class Test 
    {
        public static void main(String[] args) 
        {
            ApplicationContext context= new ClassPathXmlApplicationContext("Spring.xml");
            TeslaModelX modelX=(TeslaModelX)context.getBean("modelX");
            modelX.goForward();
        }
    }
    

    OUTPUT:

    Motor 1 Started.
    Motor 2 Started.
    Going Forward.
    

提交回复
热议问题