Bean对象属性值自动装配

Spring 应用中提供了一种机制,可以按照一定规则自动为对象属性注入值,此机制实现的可以借助Bean标签中的autowire属性进行配置.此属性的值如下几个:

  1. default : 不执行自动装配(默认)-相当于白写
  2. byName: 按配置文件中(property)的属性名对应的set方法名进行自动装配
  3. byType: 按set方法相关参数类型进行自动装配(推荐)
  4. constructor:按构造方法参数类型进行自动装配

例如:



此代码中的自动装配是按照Producer类中相关属性类型查找对应参数类型的set方法,然后再依据其类型从spring容器中查找对应类型的bean对象,最后调用对应set方法进行值的注入。
详细例子如下:


项目结构

/spring-pro-con/pom.xml


    4.0.0
    com.company
    spring-pro-con
    0.0.1-SNAPSHOT
    
        
            org.springframework
            spring-context
            4.3.9.RELEASE
        
    
    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.5.1
                
                    1.8
                    1.8
                    UTF-8
                
            
        
    

/spring-pro-con/src/main/resources/spring-configs.xml



       
       
       

/spring-pro-con/src/main/java/bean/Consumer.java

package bean;

public class Consumer {
    private Container container;

    public Consumer(Container container) {
        System.out.println("Consumer.Consumer(Container container)");
        this.container = container;
    }

    public Container getContainer() {
        return container;
    }
    
}

/spring-pro-con/src/main/java/bean/Container.java

package bean;

import java.util.List;

public class Container {
    private Listlist;
    public Container() {
        System.out.println("Container.Container()");
    }
    public List getList() {
        return list;
    }
    public void setList(List list) {
        this.list = list;
    }
}

/spring-pro-con/src/main/java/bean/Producer.java

package bean;

public class Producer {
    private Container container;

    public Container getContainer() {
        return container;
    }

    public void setContainer(Container container) {
        System.out.println("Producer.setContainer()");
        this.container = container;
    }
    
}

/spring-pro-con/src/main/java/testauto/TestAutoWire.java

package testauto;

import java.util.ArrayList;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import bean.Consumer;
import bean.Container;
import bean.Producer;

public class TestAutoWire {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext ct = new ClassPathXmlApplicationContext("spring-configs.xml");
        Container c=ct.getBean("container",Container.class);
        c.setList(new ArrayList());
        Producer p = ct.getBean("producer",Producer.class);
        p.getContainer().getList().add("cake");
        System.out.println("Producer added cake");
        Consumer consumer = ct.getBean("consumer",Consumer.class);
        String item = consumer.getContainer().getList().get(0);
        System.out.println("Consumer got "+item);
        ct.close();
    }
}

运行结果

Container.Container()
Producer.setContainer()
Consumer.Consumer(Container container)
Producer added cake
Consumer got cake

你可能感兴趣的:(Bean对象属性值自动装配)