Spring学习笔记(四)

写在前面:跟着b站视频了解一下Spring,做一点笔记

7.Bean的自动装配

1.自动装配是Spring满足bean依赖的一种方式

2.Spring会在上下文中自动寻找,并自动给bean装配属性

3.在Spring中有三种配置的方式

  • 在xml中显示的配置
  • 在java中显示配置
  • 隐式地自动装配bean[重要]

7.1.测试

1.环境搭建,一个人有两个宠物

7.2.ByName自动装配

<!--
    byName : 会自动在容器上下文中查找和自己对象set方法后面的值对应的beanid
    -->
<bean id="people" class="com.erin.pojo.People" autowire="byName">
    <property name="name" value="erin sakura"/>
</bean>

7.3.ByType自动装配

<bean class="com.erin.pojo.Cat"/>
<bean class="com.erin.pojo.Dog"/>
<!--
    byName : 会自动在容器上下文中查找和自己对象set方法后面的值对应的beanid
    byType : 会自动在容器上下文中查找和自己对象属性类型一样的bean(class),缺点:必须保证类型class全局唯一,优点:可以省略id
    -->
<bean id="people" class="com.erin.pojo.People" autowire="byName">
    <property name="name" value="erin sakura"/>
</bean>

小结:

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

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

jdk 1.5支持的注解,spring2.5就支持了

The introduction of annotation-based configuration raised the question of whether this approach is “better” than XML

要使用注解须知:

  • 导入约束:context的约束

  • 配置注解的支持: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>
    

@Autowired

直接在类的属性或属性对应的set方法上使用即可,如果在类的属性上使用,连属性对应的set方法都不用声明了,但前提是这个自动装配的属性在IoC(Spring)容器中存在,且符合类型byType。

注意:

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

Autowired测试代码:

public class People {
    private String name;
    // 如果显示定义了Autowired的required属性为false,说明这个对象可以为null,否则不允许为空
    @Autowired(required = false)
    private Cat cat;
    @Autowired
    private Dog dog;
}

如果@Autowired自动装配的环境比较复杂,自动装配无法通过一个注解@Autowired实现,即beans.xml中有多个类型(class)相同的对象,则@Autowired可能无法生效,此时需要在属性上添加类似@Qualifier(value="dog222")来指定容器中对应的唯一的bean id为"dog222"的对象。

public class People {
    @Autowired
    @Qualifier(value="cat11")
    private Cat cat;
    @Autowired
    @Qualifier(value="dog222")
    private Dog dog;
}

@Resource

是java的注解,只能用于容器中每个类型只有一个对象的情况,会先按照对象属性的名字查找容器中对应的id,然后按照对象属性的类型查找容器中对应的class,如果这二者查找都没有找到对应的bean对象则报错。

如果希望能在容器中有多个类型一样但id不一样的bean对象的情况下,使用@Resource,需要搭配@Resource(name = “cat1”),这样就能在容器中找到id为cat1的bean对象

public class Student{
    @Resource(name="cat1")
    private Cat cat;
    @Resource
    private Dog dog;
}
<bean id="cat1" class="com.erin.pojo.Cat"/>
<bean id="cat11" class="com.erin.pojo.Cat"/>
<bean id="dog222" class="com.erin.pojo.Dog"/>
<bean id="people" class="com.erin.pojo.People" />

小结:

@Resource和@Autowired的区别:

  • 都是用于自动装配的,都可以放在属性字段上
  • @Autowired通过byType的方式实现,但如果有相同类型会通过byName的方式注入,而且必须要求这个对象存在(常用)(参考博客理解:https://blog.csdn.net/weixin_40698359/article/details/86065031)
  • @Resource默认通过byName的方式实现,如果找不到名字,则通过byType实现,如果两个都找不到的情况下就报错(常用,类似于autowired和qualifier的集合体)
  • 执行顺序不同
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值