Spring——Bean的自动装配

在这里插入图片描述

  • 自动装配是Spring满足bean依赖的一种方式
  • Spring会在上下文中自动寻找,并自动给bean装配属性

Spring中有三种装配方式

  • 1、在xml中显示的配置
  • 2、在Java中显示的配置
  • 3、隐式的自动装配bean(重点)

测试

要求:一个人有两个宠物

1.新建spring-05-Autowired模型

在这里插入图片描述

2.新建com.kuang.pojo包存放实体类对象

Cat类:

package com.kuang.pojo;

public class Cat {
 

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

Dog类:

package com.kuang.pojo;

public class Dog {

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

People类:

import com.kuang.pojo.People;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class myTest {

    @Test
    public void test(){
     ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        People people = context.getBean("people", People.class);
//        System.out.println(people);
        people.getCat().shout();
        people.getDog().shout();
    }
}

在这里插入图片描述

3.创建配置文件beans.xml

3.1 手动装配方式
<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

<!--手动装配-->
    <bean id="people" class="com.kuang.pojo.People">
        <property name="name" value="晓晓"></property>
        <property name="dog" ref="dog"></property>
        <property name="cat" ref="cat"></property>
    </bean>
    <bean id="dog" class="com.kuang.pojo.Dog">
        
    </bean>
    <bean id="cat" class="com.kuang.pojo.Cat">
        
     </bean>

</beans>

在这里插入图片描述

3.2自动装配方式:ByName
<!--    自动装配,通过byName-->
<!--    byName:
                会自动在容器上下文查找,和自己对象set方法后面的值对应的beanId
-->
    <bean id="people" class="com.kuang.pojo.People" autowire="byName">
        <property name="name" value="晓晓"></property>
    </bean>
<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

<!--    自动装配,通过byName-->
<!--    byName:
                会自动在容器上下文查找,和自己对象set方法后面的值对应的beanId-->
    <bean id="dog" class="com.kuang.pojo.Dog" />
    <bean id="cat" class="com.kuang.pojo.Cat" />
    <bean id="people" class="com.kuang.pojo.People" autowire="byName">
        <property name="name" value="晓晓"></property>
    </bean>

</beans>

在这里插入图片描述
如果不对应会出现空指针异常
在这里插入图片描述

3.3自动装配方式:byType

要保证类型全局唯一

   <!--    自动装配,通过byType-->
    <!--    byType:
                    会自动在容器上下文查找,和自己对象属性类型相同的bean
                    保证类型全局唯一
                    可以不填对象id,因为根据的是对象的类型-->
    <bean  class="com.kuang.pojo.Dog" />
    <bean  class="com.kuang.pojo.Cat" />
    <bean id="people" class="com.kuang.pojo.People" autowire="byType">
        <property name="name" value="晓晓"></property>
    </bean>

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--    自动装配,通过byType-->
    <!--    byType:
                    会自动在容器上下文查找,和自己对象属性类型相同的bean
                    保证类型全局唯一
                    可以不填对象id,因为根据的是对象的类型-->
<!--    <bean id="dog" class="com.kuang.pojo.Dog" />-->
<!--    <bean id="cat" class="com.kuang.pojo.Cat" />-->
    <bean  class="com.kuang.pojo.Dog" />
    <bean  class="com.kuang.pojo.Cat" />
    <bean id="people" class="com.kuang.pojo.People" autowire="byType">
        <property name="name" value="晓晓"></property>
    </bean>



</beans>

在这里插入图片描述

3.4自动装配方式小结:
  • byName时,要保证所有bean的id唯一,并且这个bean需要和自动注入的属性的set方法值一致
  • byType时,要保证bean的class唯一,并且这个bean需要和自动注入的属性的类型一致
3.5 注解实现自动装配

jdk1.5支持的注解,Spring2.5就开始支持注解了
在这里插入图片描述

要使用注解须知:
1.导入约束。context约束
2.配置注解的支持:context:annotation-config/

<?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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

<!--    开启注解支持-->
    <context:annotation-config/>

</beans>
3.5.1@Autowired
  • 直接在属性上使用即可!也可以在set方式上使用
  • 使用Autowired我们可以不再编写Set方法了,前提是你这个自动装配的属性在IOC(Spring)容器中存在,且符合名字byName


    科普:
@Nullable      字段标记了这个注解,说明这个字段可以为null
public @interface Autowired {
    boolean required() default true;
}

People对象:

package com.kuang.pojo;

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

public class People {

//    如果显示定义了Autowired的required属性为false,说明这个对象可以为null,否则不允许为空
    @Autowired(required = false)
    private Cat cat;
    @Autowired
    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 "People{" +
                "cat=" + cat.toString() +
                ", dog=" + dog.toString() +
                ", name='" + name + '\'' +
                '}';
    }
}

beans.xml配置文件

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">


<!--    开启注解支持-->
    <context:annotation-config/>

<!--    注解装配-->
    <bean id="dog"  class="com.kuang.pojo.Dog" />
    <bean id="cat"  class="com.kuang.pojo.Cat" />
    <bean id="people" class="com.kuang.pojo.People" />




</beans>

3.5.2 如果@Autowired自动装配的环境比较复杂(比如bean的类型不唯一时),自动装配无法通过一个注解【@Autowired】完成时,我们可以使用@Qualifier(value=“xxx”)去配置@Autowired使用,指定一个唯一的bean对象注入

例:

people类
package com.kuang.pojo;

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

public class People {


    @Autowired
    //我们可以使用@Qualifier(value="xxx")去配置@Autowired使用,指定一个唯一的bean对象注入
    @Qualifier(value = "cat1")
    private Cat cat;
    @Autowired
    @Qualifier(value = "dog1")
    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 "People{" +
                "cat=" + cat.toString() +
                ", dog=" + dog.toString() +
                ", name='" + name + '\'' +
                '}';
    }
}

beans.xml配置
<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

<!--    开启注解支持-->
    <context:annotation-config/>

<!--    注解装配-->
<!--   下面bean的类型不唯一-->
    <bean id="dog1"  class="com.kuang.pojo.Dog" />
    <bean id="cat1"  class="com.kuang.pojo.Cat" />
    <bean id="dog22"  class="com.kuang.pojo.Dog" />
    <bean id="cat22"  class="com.kuang.pojo.Cat" />
    <bean id="people" class="com.kuang.pojo.People" />




</beans>

3.5.3 @Resource注解:

●要么类型唯一
●要么名字匹配

//可以通过加name来指定名字
 @Resource(name = "cat12")
People类
package com.kuang.pojo;

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

import javax.annotation.Resource;

public class People {

    @Resource
    private Cat cat;

    @Resource
    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 "People{" +
                "cat=" + cat.toString() +
                ", dog=" + dog.toString() +
                ", name='" + name + '\'' +
                '}';
    }
}

beans.xml
<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">


<!--    开启注解支持-->
    <context:annotation-config/>

<!--    注解装配-->
<!--    使用@Resource时类型唯一,名字可以不匹配-->
<!--    <bean id="dog"  class="com.kuang.pojo.Dog" />-->
<!--    <bean id="cat1"  class="com.kuang.pojo.Cat" />-->
<!--    <bean id="people" class="com.kuang.pojo.People" />-->

    <!--    使用@Resource时类型不唯一,则至少要有一组名字匹配-->
    <bean id="dog"  class="com.kuang.pojo.Dog" />
    <bean id="cat"  class="com.kuang.pojo.Cat" />
    <bean id="dog22"  class="com.kuang.pojo.Dog" />
    <bean id="cat22"  class="com.kuang.pojo.Cat" />
    <bean id="people" class="com.kuang.pojo.People" />





</beans>

3.5.4 小结
  • @Resource和@Autowired的区别:
    相同点:
    ●都是用来自动装配的,都可以放在属性字段上
    不同点:
    ●@Autowired通过byType的方式实现,而且必须要求这个对象存在
    ●@Resource默认通过byName方式实现,如果找不到名字,则通过byType实现!如果两个都找不到,就会报错
    ●执行顺序不同:
    @Autowired通过byType的方式实现。
    @Resource默认通过byName方式实现

4.创建测试类

import com.kuang.pojo.People;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class myTest {

    @Test
    public void test(){
//        获取Spring容器
     ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        People people = context.getBean("people", People.class);
//        System.out.println(people);
        people.getCat().shout();
        people.getDog().shout();
    }
}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值