【java spring】自动装配

bean的自动装配可以让框架自动为我们注入依赖。自动装配不能够装配基本类型,只能把一个bean装配到另一个bean中。

使用自动装配需要配置 元素的autowire属性。

当autowire=no时,表示不使用自动装配,Bean依赖必须通过ref元素定义。
当autowire=byName时,根据Property的name自动装配,如果一个Bean的name(或id)和同一xml中另一个Bean中的Property的name相同,则自动装配这个Bean到Property中。
当autowire=byType时,根据Property的数据类型(Type)自动装配,如果一个Bean的数据类型兼容另一个Bean中Property的数据类型,则自动装配。
当autowire=constructor时,类似于 byType,根据构造方法参数的数据类型,进行 byType 模式的自动装配。
当autowire=autodetect时(3.0版本不支持,并且博主的版本里没这个,就介绍下),如果Bean中有默认的构造方法,则用constructor模式,否则用byType模式。

下面测试一下:

public class Person
{
    private Man man;
    private Man man1;
    public Person() {
        System.out.println("在Person的构造函数内");
    }
    public Person(Man man) {
        System.out.println("在Person的有参构造函数内");
        this.man = man;
    }
    public void man() {
        man.show();
    }
    public Man getMan() {
        return man;
    }
    public void setMan(Man man) {
        this.man = man;
    }
}

public class Man
{
    private String name;
    private int age;
    public Man() {
        System.out.println("Man()");
    }
    public Man(String name, int age) {
        System.out.println("Man(String name, int age)");
        this.name = name;
        this.age = age;
    }
    public void show() {
        System.out.println("name:" + name + "\nage:" + age);
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}

public class AutoInjectTest
{
    public ApplicationContext context;
    @Before
    public void getContext()
    {
        context= new ClassPathXmlApplicationContext("auto_inject.xml");
    }
    @Test
    public void test()
    {
        System.out.println("----person1----");
        Person person1=(Person) context.getBean("person1");
        person1.man();
        System.out.println("----person2----");
        Person person2=(Person) context.getBean("person2");
        person2.man();
        System.out.println("----person3----");
        Person person3=(Person) context.getBean("person3");
        person3.man();
    }
}
<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.xsd">
    <bean id="man" class="upc.yqs.Man">
        <constructor-arg name="age" value="10"></constructor-arg>
        <constructor-arg name="name" value="yqs"></constructor-arg>
    </bean>
    <bean id="person1" class="upc.yqs.Person" autowire="byName">
    </bean>
    <bean id="person2" class="upc.yqs.Person" autowire="byType">
    </bean>
    <bean id="person3" class="upc.yqs.Person" autowire="constructor">
    </bean>
</beans>

输出:

Man(String name, int age)Person的构造函数内
在Person的构造函数内
在Person的有参构造函数内
----person1----
name:yqs
age:10
----person2----
name:yqs
age:10
----person3----
name:yqs
age:10
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值