spring中IoC中的自动装配

Bean注入管理方式成为bean的装配策略。
IoC容器可以自动装配autowire相互协作bean之间的关联关系。可以让BeanFactory中的内容来替代我们指定的bean的协作者。
使用自动装配可以使配置文件减少。

为了是大家能更好的的理解自动装配的原理,我用代码向大家,解释效果能更为直观。
1.在不使用自动装配的情况下。使用p命名空间

首先定义两个类。

类B1

public class B1 {

}

类A1使用B1这个对象

public class A1 {
    private B1 b1;
    public A1(){}
    public A1(B1 b1){
        System.out.println("A1的构造器");
        this.b1 = b1;
    }
    public void pp(){
        System.out.println("this is A1.class");
        System.out.println("调用B的方法"+b1);
    }
    public B1 getB1() {
        System.out.println("get方法");
        return b1;
    }
    public void setB1(B1 b1) {
        System.out.println("set方法");
        this.b1 = b1;
    }
}

配置文件

使用p名空间

<?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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"
    default-autowire="constructor">
    <bean id="aa" class="com.jia.A2.A1" p:bb-ref="bb"/>
    <bean id="bb" class="com.jia.A2.B1"/>
</beans>

测试上述文件代码

public class test {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
        A1 a1 = applicationContext.getBean("aa",A1.class);
        a1.pp(); 
    }
}

测试结果

A1的构造器
set方法
this is A1.class
调用B的方法com.jia.A2.B1@1a9ff430

在使用autowire的情况下。

autowire的五种属性解析。

  1. byType
    按照属性类型进行自动装配[使用的是设置器注入],如果A类中有个类型为B的属性,如果受管bean中有对应类型的对象,则容器自动调用set方法设置A对象的这个属性,不管名称是否对应
<?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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"
    default-autowire="byType">
    <bean id="aa" class="com.jia.A2.A1" autowire="default"/>
    <bean id="bb" class="com.jia.A2.B1"/>
</beans>

运行结果

set方法
this is A1.class
调用B的方法com.jia.A2.B1@7e2b2718

2.byName
意思是采用名称自动装配[使用的是设置器注入],A中有个属性叫b,如果当前配置中有一个受管bean名称为b,则系统自动调用set方法将其进行注入到A对象中

<?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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"
    default-autowire="byType">
    <bean id="aa" class="com.jia.A2.A1" autowire="byName"/>
    <bean id="bb" class="com.jia.A2.B1"/>
</beans>

运行结果

this is A1.class
调用B的方法null
//没调取到B1的方法是因为A中属性叫b1,而配置中的受管bean名称为bb所以自动装配无法识别

3.contructor
按照构造器进行自动装配

<?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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"
    default-autowire="byType">
    <bean id="aa" class="com.jia.A2.A1" autowire="contructor"/>
    <bean id="bb" class="com.jia.A2.B1"/>
</beans>

运行结果

A1的构造器
this is A1.class
调用B的方法com.jia.A2.B1@67e9d110

4.default
默认自动装配
在配置文件的根标签上有一个配置属性
default-autowire=”no/byType/byName/contructor”可以定义当前配置文件中的默认装配方法

<?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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"
    default-autowire="byType">
    <bean id="aa" class="com.jia.A2.A1" autowire="default"/>
    <bean id="bb" class="com.jia.A2.B1"/>
</beans>

运行结果

set方法
this is A1.class
调用B的方法com.jia.A2.B1@7e2b2718
//这里我使用默认的自动装配,而自动装配是byType,调用了set方法

4.no
默认不使用自动装配

一般情况下,在实际的项目中很少使用自动装配功能,因为和自动装配功能所带来的好处比起来,明确清晰的配置文档更有说服力一些

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值