Spring 笔记 - 07 bean的自动装配

7. bean的自动装配

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

在Spring中有三种装配方式:

  1. 在xml中显式配置
  2. 在java中显式配置
  3. 隐式的自动装配bean
7.1 测试
  1. 环境搭建

    • 一个人有两个宠物

      public class Cat {
      
          public void shout() {
              System.out.println("miao....");
          }
      }
      
      public class Dog {
      
          public void shout() {
              System.out.println("wang...");
          }
      }
      
      public class People {
          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() {
              final StringBuilder sb = new StringBuilder("People{");
              sb.append("cat=").append(cat);
              sb.append(", dog=").append(dog);
              sb.append(", name='").append(name).append('\'');
              sb.append('}');
              return sb.toString();
          }
      }
      
    • beans.xml

      <bean id="cat" class="com.javacto.pojo.Cat"/>
      <bean id="dog" class="com.javacto.pojo.Dog"/>
      
      <bean id="people" class="com.javacto.pojo.People">
          <property name="name" value="eric"/>
          <property name="cat" ref="cat"/>
          <property name="dog" ref="dog"/>
      </bean>
      
    • 测试:

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

      image-20210916112731094

7.2 Autowire 自动装配
  • ByName 自动装配

    <bean id="cat" class="com.javacto.pojo.Cat"/>
    <bean id="dog" class="com.javacto.pojo.Dog"/>
    
    <!-- ByName: 会自动在上下文容器中寻找,和自己对象set方法后面的值对应的 bean id -->
    <bean id="people" class="com.javacto.pojo.People" autowire="byName">
        <property name="name" value="eric"/>
    </bean>
    
  • ByType 自动装配

    <bean id="cat" class="com.javacto.pojo.Cat"/>
    <bean id="dog" class="com.javacto.pojo.Dog"/>
    
    <!-- ByName: 会自动在上下文容器中寻找,和自己对象set方法后面的值对应的 bean id -->
    <!-- ByType: 会自动在上下文容器中寻找,和自己对象属性类型相同的bean  -->
    
    <bean id="people" class="com.javacto.pojo.People" autowire="byType">
        <property name="name" value="eric"/>
    </bean>
    
  • 小结:

    • ByName : 需要保证 bean 的 id 唯一 !
    • ByType: 需要保证 bean 类型唯一 !
7.3 使用注解实现bean的自动装配
  • jdk 1.5 支持的注解, spring2.5 就支持注解了 !

  • 要使用注解须知:

    1. 导入约束:

      <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"
             xmlns:aop="http://www.springframework.org/schema/aop"
             xsi:schemaLocation="http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans.xsd
             http://www.springframework.org/schema/context
             http://www.springframework.org/schema/context/spring-context.xsd
             http://www.springframework.org/schema/aop
             http://www.springframework.org/schema/aop/spring-aop.xsd">
          
      </beans>
      
    2. 配置注解支持

      <!-- 配置注解支持 -->
      <context:annotation-config />
      
  • @Autowired

    直接使用在属性上即可, 也可以在set方式上使用

    这个注解就相当于bean标签 属性里的 autowire

    image-20210916115803957

科普:

@Nullable  在属性上使用了这个注解,就是说这个属可以为null
@Autowired(requied = false)  false就是与@Nullable作用一样

如果Autowired 自动装配的环境较为复杂,没有与属性名对应的id,类型也不唯一,

这是后我们可以是用@Qualifier (value=“bean 的id”) 配合使用,指定唯一的对象注入

image-20210916170858440

@Resource : 作用与 @Autowired 一样,但它不是spring注解,属于java 提供的注解,需要注意,JDK8后的版本需要添加依赖才能使用。

image-20210916171655658

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值