码迷,mamicode.com
首页 > 编程语言 > 详细

Spring自动装配Beans

时间:2017-07-13 20:31:46      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:代码   schema   strong   赋值   cto   processor   []   size   work   

学习来源:  http://www.yiibai.com/spring/spring-auto-wiring-beans-in-xml.html

       http://www.yiibai.com/spring/spring-auto-wiring-beans-with-autowired-annotation.html

       

自动装配(autowiring),我是这样理解的:bean1作为bean2的属性,在配置文件中设置二者的关联后,当Spring容器创建bean2对象时,会自动创建bean1的对象并赋值给bean1对象的相关属性。

1. 自动装配模式

自动装配模式有五种,通过在<bean>中定义autowire属性进行选择。

① 不定义该属性:即通过 ref 属性进行手动设置。

 

② autowire="byName"

 

③ autowire="byType"

 

④ autowire="constructor"

 

⑤ autowire="autodetect"

 

2. 使用注解自动注入: @Autowired,通过匹配数据类型自动装配Bean

无需在配置文件中设置关联,仅需在主动引用方的类中,在需要自动装配的位置添加 @Autowired 即可。

可以添加注解的位置有三种:setter方法,构造函数,字段(即属性)。

如果在配置文件中没有找到相应的Bean,自动装配显然会出错,这是因为 @Autowired 默认执行依赖检查,当自动装配失败时,会抛出异常。

如果不想检查直接装配,可手动设置 @Autowired(required=false),requeired属性默认为true,表示是否需要执行依赖检查。(不执行依赖检查,切装配失败时,相应属性不予设定)

/**
* @Autowired   setter方法
*/
package com.yiibai.common;

import org.springframework.beans.factory.annotation.Autowired;

public class Customer 
{
    private Person person;
    private int type;
    private String action;
    //getter and setter methods
    
    @Autowired
    public void setPerson(Person person) {
        this.person = person;
    }
}
/**
* @Autowired   构造方法
*/
package com.yiibai.common;

import org.springframework.beans.factory.annotation.Autowired;

public class Customer 
{
    private Person person;
    private int type;
    private String action;
    //getter and setter methods
    
    @Autowired
    public Customer(Person person) {
        this.person = person;
    }
}
/**
* @Autowired   字段
*/
package com.yiibai.common;

import org.springframework.beans.factory.annotation.Autowired;

public class Customer 
{
    @Autowired
    private Person person;
    private int type;
    private String action;
    //getter and setter methods
}
/**
* 测试
*/
package com.yiibai.common;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App 
{
    public static void main( String[] args )
    {
        ApplicationContext context = 
          new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});
        
        Customer cust = (Customer)context.getBean("CustomerBean");
        System.out.println(cust);
        
    }
}

注意:要想启用@Autowired,必须先注册AutowiredAnnotationBeanPostProcessor。

注册方式有两种:

①直接在配置文件中包含‘AABPP’,即添加下行代码:

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> <bean id="CustomerBean" class="com.yiibai.common.Customer"> <property name="action" value="buy" /> <property name="type" value="1" /> </bean> <bean id="PersonBean" class="com.yiibai.common.Person"> <property name="name" value="yiibai" /> <property name="address" value="address ABC" /> <property name="age" value="29" /> </bean> </beans>

② include <context:annotation-config />

<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-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <context:annotation-config />

    <bean id="CustomerBean" class="com.yiibai.common.Customer">
        <property name="action" value="buy" />
        <property name="type" value="1" />
    </bean>

    <bean id="PersonBean" class="com.yiibai.common.Person">
        <property name="name" value="yiibai" />
        <property name="address" value="address ABC" />
        <property name="age" value="29" />
    </bean>
</beans>

 

3. @Qualifier,通过匹配Bean的id,对Bean进行自动装配

由于 @Autowired 是通过匹配数据类型来自动装配Bean,所有当被装配的实体类有具多个bean时,@Autowired 是无法得知装配哪个bean的。

这时就要使用@Qualifier,较之匹配数据类型的 @Autowired,@Qualifier是对<bean>节点的 id 进行匹配。

package com.yiibai.common;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class Customer 
{
    @Autowired
    @Qualifier("PersonBean1")
    private Person person;
    private int type;
    private String action;
    //getter and setter methods
}

 

Spring自动装配Beans

标签:代码   schema   strong   赋值   cto   processor   []   size   work   

原文地址:http://www.cnblogs.com/zhengmengen/p/7161805.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!