(修改完成)spring 梳理6-- 注解开发 (一) 扫包(必要操作)、 自动装配 @Autowired @Qualifier @Resource

@Autowired

@Qualifier

@Resource

 

 

 

(一)扫包:

修改文件,在user的address属性上加注解@Autowired

@Autowired
private Address address;

 

删除user配置文件中的address注入

<!-- 引用类型注入 -->
<property name="address" ref="address"/>

 

测试失败,发现注解没有生效,address是null。

 

 

思考

注解只是个标识,我们需要去解析他,但是解析就要在运行前看一看。

所以要让注解生效,还需要进行一个配置,在运行前,扫描一下所有的文件,看看哪些类头上戴了我的注解

 

 

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

 

 

2、在配置文件中加次注解,就是告诉spring【com.xinzhi】下的类都扫描一下看看

<context:component-scan base-package="com.xinzhi"/>

 

 

3、测试

User(name=孙锐, address=Address(addressInfo=南京), hobbies=[sing, dance, rap, basketball], duties=[班长, 学习委员], familyTies={father=父亲, mother=母亲}, carts=[笔, 衣服, 鞋子], workExperience={第一年=实习了, 第二年=正式工作了}, dog=Dog(type=金毛))

 

 

 

 

(二)自动装配

 

(1)

@Autowired  

@Autowired是按类型自动转配的,不支持id匹配。

需要导入 spring-aop的包!

 

 见开头的例子

 

 

(2)

@Qualifier

@Qualifier不能单独使用,它和@Autowired配合可以根据名称进行自动装配

 

测试实验步骤:

1、配置文件中的address名字为address2

<bean id="dog1" class="com.kuang.pojo.Dog"/>
<bean id="dog2" class="com.kuang.pojo.Dog"/>
<bean id="cat1" class="com.kuang.pojo.Cat"/>
<bean id="cat2" class="com.kuang.pojo.Cat"/>

 

2、没有加Qualifier测试,直接报错

 

3、在属性上添加Qualifier注解

@Autowired
@Qualifier(value = "cat2")
private Cat cat;
@Autowired @Qualifier(value
= "dog2") private Dog dog;

 

 

 

 

 (3)

@Resource

① @Resource如有指定的name属性,先按该属性进行byName方式查找装配;

② 其次再进行默认的byName方式进行装配;

③ 如果以上都不成功,则按byType的方式自动装配。

④ 都不成功,则报异常。

 

 

 实体类:

public class User {
  //如果允许对象为null,设置required = false,默认为true
  @Resource(name = "cat2")
  private Cat cat;
  @Resource   
private Dog dog;   private String str; }

 

 beans.xml

<bean id="dog" class="com.kuang.pojo.Dog"/>
<bean id="cat1" class="com.kuang.pojo.Cat"/>
<bean id="cat2" class="com.kuang.pojo.Cat"/>
<bean id="user" class="com.kuang.pojo.User"/>

 

测试:结果OK

 

 

但如果:beams.xml,删掉cat2

<bean id="dog" class="com.kuang.pojo.Dog"/>
<bean id="cat1" class="com.kuang.pojo.Cat"/>

 

实体类上只保留注解

@Resource
private Cat cat;
@Resource
private Dog dog;

 

 

结果:OK

结论:先进行byName查找,失败;再进行byType查找,成功

 

 

@Autowired与@Resource异同:

1、@Autowired与@Resource都可以用来装配bean。都可以写在字段上,或写在setter方法上。

2、@Autowired默认按类型装配(属于spring规范),默认情况下必须要求依赖对象必须存在,如果要允许null 值,可以设置它的required属性为false,如:@Autowired(required=false) ,如果我们想使用名称装配可以结合 @Qualifier注解进行使用

3、@Resource(属于J2EE复返),默认按照名称进行装配,名称可以通过name属性进行指定。如果没有指定 name属性,当注解写在字段上时,默认取字段名进行按照名称查找,如果注解写在setter方法上默认取属性名进 行装配。当找不到与名称匹配的bean时才按照类型进行装配。但是需要注意的是,如果name属性一旦指定,就只会按照名称进行装配。

它们的作用相同都是用注解方式注入对象,但执行顺序不同。@Autowired先byType,@Resource先byName

 

posted @ 2021-01-23 15:43  Master_Sun  阅读(180)  评论(0)    收藏  举报