【Spring学习笔记】07、Bean的自动装配

07、Bean的自动装配

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

在Spring中有三种装配的方式

  1. 在xmI中显式的配置
  2. 在java中显式配置
  3. 隐式的自动装配bean【重要】

7.1、测试

环境搭建:一个人有两个宠物!

public class People {
   private Cat cat;
   private Dog dog;
   private String name;
}// 省略get,set,toString方法

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

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

7.2、byName自动装配

    <bean id="cat" class="com.haining820.pojo.Cat"/>
    <bean id="dog" class="com.haining820.pojo.Dog"/>
	<!--不能随便修改id,byName找不到对象会报空指针异常-->
	<!--<bean id="dog666" class="com.haining820.pojo.Dog"/>-->

    <!--byName:会自动在容器的上下文中查找和自己对象set方法后面的值对应的bean id!-->
    <bean id="people" class="com.haining820.pojo.People" autowire="byName">
        <property name="name" value="yuhaiyang"/>
        <!--使用byName可以省略-->
        <!--<property name="dog" ref="dog"/>-->
        <!--<property name="cat" ref="cat"/>-->
    </bean>

7.3、byType自动装配

	<!--<bean id="cat" class="com.haining820.pojo.Cat"/>-->
    <!--<bean id="dog666" class="com.haining820.pojo.Dog"/>-->
	<!--因为不需要名字,byType修改id或者甚至省略id-->
    <bean class="com.haining820.pojo.Cat"/>
    <bean class="com.haining820.pojo.Dog"/>

    <!--byType:会自动在容器的上下文中查找和自己对象属性类型相同的bean id!-->
	<!--byType必须保证属性的类型全局唯一才能自动装配,否则会报错!-->
    <bean id="people" class="com.haining820.pojo.People" autowire="byType">
        <property name="name" value="yuhaiyang"/>
        <!--<property name="dog" ref="dog"/>-->
        <!--<property name="cat" ref="cat"/>-->
    </bean>

装配后的测试

 	@Test
    public void test1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        People people = context.getBean("people", People.class);
        people.getCat().shout();
        people.getDog().shout();
    }//输出miao~ wang~

小结:

  • 使用byName时,需要保证所有bean的id唯一,并且这个bean需要和自动注入的属性的set方法的值一致!
  • 使用byType时,需要保证所有bean的class唯一,并且这个bean需要和自动注入的属性的类型一致!

7.4、使用注解实现自动装配

jdk1.5开始支持注解,Spring2.5开始支持注解,注解比xml文件更方便。

使用注解的注意事项:

  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/>

    <bean id="cat" class="com.haining820.pojo.Cat"/>
    <bean id="dog" class="com.haining820.pojo.Dog"/>
    <bean id="people" class="com.haining820.pojo.People"/>

</beans>
  • 使用@Autowired

    • 直接在属性上使用即可,也可以在Set方法上使用!

    • 使用Autowired后就可以不用编写Set方法了,前提是这个自动装配的属性在IoC (Spring) 容器中存在,且符合类型byType!

    • 先 byType ,要是有多个同类型的,就 byName,以下bean的配置都可以成功运行

        	<!--byType-->
        	<bean id="cat1" class="com.haining820.pojo.Cat"/>
            <bean id="dog1" class="com.haining820.pojo.Dog"/>
            <bean id="people" class="com.haining820.pojo.People"/>
      
        	<!--byName-->
        	<bean id="cat1" class="com.haining820.pojo.Cat"/>
            <bean id="cat" class="com.haining820.pojo.Cat"/> <!--byName-->
            <bean id="dog1" class="com.haining820.pojo.Dog"/>
            <bean id="dog" class="com.haining820.pojo.Dog"/> <!--byName-->
            <bean id="people" class="com.haining820.pojo.People"/> <!--byType-->
      
        public class People {
           @Autowired
           private Cat cat;
           @Autowired
           private Dog dog;
           private String name;
        }
      
    • @Autowired的require属性

        public @interface Autowired {
            boolean required() default true;
            //源码中required默认为true
        }
      
        public class People {
            //如果显式的定义了Autowired的required属性为false,说明这个对象可以为nu11,否则不允许为空
            @Autowired(required = false)
           private Cat cat;
            @Autowired
           private Dog dog;
           private String name;
        }
      
  • 使用@Qualifier

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

      public @interface Qualifier {
          String value() default "";
          //源码中value默认为空
      }
    
          <!--需要@Qualifier-->
      	<bean id="cat1" class="com.haining820.pojo.Cat"/> <!--@Qualifier-->
          <bean id="cat2" class="com.haining820.pojo.Cat"/>
          <bean id="dog1" class="com.haining820.pojo.Dog"/> <!--@Qualifier-->
          <bean id="dog2" class="com.haining820.pojo.Dog"/>
          <bean id="people" class="com.haining820.pojo.People"/>
    
      public class People {
         @Autowired
         @Qualifier(value = "cat1")
         private Cat cat;
         @Autowired
         @Qualifier(value = "dog1")
         private Dog dog;
         private String name;
      }
    
  • @Resource注解

      public class People {
          @Resource
         private Cat cat;
          @Resource
         private Dog dog;
         private String name;
      }
    

    @Resource也有类似@Qualifier的用法

      public class People {
          @Resource(name = "cat2")
         private Cat cat;
          @Resource(name = "dog2")
         private Dog dog;
         private String name;
      }
    

@Resource 和 @Autowired的区别

  • 都是用来自动装配的,都可以放在属性字段上
  • @Autowired 默认通过 byType 的方式实现,先 byType 后 byName!而且必须要求这个对象存在! 【常用】
  • @Resource 默认通过 byName 的方式实现,先 byName 后 byType!如果两个都找不到,就报错! 【常用】
  • 执行顺序不同:@Autowired默认通过 byType 的方式实现。@Resource默认通过 byName 的方式实现。

科普:@Nullable,字段标记了这个注解,说明这个字段可以为 null 。

本文章内容整理自b站up主:遇见狂神说,欢迎关注,一键三连!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值