Spring 5 Bean的自动装配(三)

自动装配是Spring满足bean依赖一种方式!
Spring会在上下文中自动寻找,并自动给bean装配属性!
在Spring中有三种自动装配的方式
1.在xml中显示的配置
2.在java中显示配置
3.隐式的自动装配bean(重点,必须掌握)

1.搭建环境:一个人两个宠物
先创建两个宠物

public class Cat {
    public void shout(){
        System.out.println("miao miao ~");
    }
}

public class Dog {
    public void shout(){
        System.out.println("wang wang ~");
    }
}

再创建人

public class Person {
    private Cat cat;
    private Dog dog;
    private String name;

    public Cat getCat() {
        return cat;
    }

    public void setCat(Cat cat) {
        this.cat = cat;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Person{" +
                "cat=" + cat +
                ", dog=" + dog +
                ", name='" + name + '\'' +
                '}';
    }
}

写配置文件

<?xml version="1.0" encoding="UTF-8"?>
<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 = "cat" class="xinan.Spring.Cat"></bean>
    <bean id = "dog" class="xinan.Spring.Dog"></bean>
    <bean id="people" class="xinan.Spring.Person">
        <property name="name" value="哈哈"></property>
        <property name="cat" ref="cat"></property>
        <property name="dog" ref="dog"></property>
    </bean>
</beans>

测试


public class Mytest {
    @Test
    public void test1(){
        ApplicationContext Context = new ClassPathXmlApplicationContext("applicationContext.xml");
       Person people = (Person) Context.getBean("people");
       people.getCat().shout();
       people.getDog().shout();
    }
}

不难发现在这里我们使用的是之前的显示装配。
1.1 ByName自动装配
将上面的配置文件改成如下形式

 <bean id = "cat" class="xinan.Spring.Cat"></bean>
    <bean id = "dog" class="xinan.Spring.Dog"></bean>
    <bean id="people" class="xinan.Spring.Person" autowire="byName">
        <property name="name" value="哈哈"></property >

    </bean>

通过设置autowire的属性,我们可以不用写其他两个宠物的属性。
byname:会自动在容器上下文中查找和自己对象set方法后面的值对应的bean id,此时id只能为set后面部分且首字母小写,且id必须唯一。
1.2 ByType自动装配

 <bean id = "cat11" class="xinan.Spring.Cat"></bean>
    <bean id = "dog" class="xinan.Spring.Dog"></bean>
    <bean id="people" class="xinan.Spring.Person" autowire="byType">
        <property name="name" value="哈哈"></property >

    </bean>

byType:会自动在容器上下文中查找,和自己对象属性类型相同的bean !必须保证其中的类型全局唯一。此时对id的名字没有要求。此时可以不用写bean id的值。

 <bean  class="xinan.Spring.Cat"></bean>
    <bean  class="xinan.Spring.Dog"></bean>
    <bean id="people" class="xinan.Spring.Person" autowire="byType">
        <property name="name" value="哈哈"></property >

    </bean>

2.使用注解实现自动装配(重点 )
使用注解须知:首先导入下面的1和2。
1.导入约束。context约束。如下图中划线的地方。
2.配置注解的支持 :<context:annotation-config></context:annotation-config>
在这里插入图片描述
此时里面得配置文件如下:

 <bean id = "cat" class="xinan.Spring.Cat"></bean>
    <bean id = "dog" class="xinan.Spring.Dog"></bean>
    <bean id="people" class="xinan.Spring.Person" ></bean>

@Autowired
直接在属性上使用即可!也可以在set方式上使用!
使用Autowired我们可以不用编写Set方法了,前提是你这个自动装配的属性在IOC (Spring)容器中存在。@Autowired注入首先根据byType注入,当拥有类型相同数量大于1时在根据byName注入。
如果显示定义了Autowired的required属性为false,说明这个对象可以为null,否则不允许为空。默认情况下require的属性为true。

比如:


public class Person {
    @Autowired
    private Cat cat;
    @Autowired
    private Dog dog;
    private String name;

    public Cat getCat() {
        return cat;
    }

    public Dog getDog() {
        return dog;
    }
    public String getName() {
        return name;
    }
    @Override
    public String toString() {
        return "Person{" +
                "cat=" + cat +
                ", dog=" + dog +
                ", name='" + name + '\'' +
                '}';
    }
}

如果@Autowired自动装配的环境比较复杂,自动装配无法通过一个注解【@Autowired】完成的时候、我们可以
使用@Qualifier(value=“xxx”)去配合@Autowired的使用,指定一个唯一的bean对象注入!
使用@Autowired或者@Autowired+@Qulifier或者@Resource进行userDao的注入
在这里插入图片描述

`

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值