【Spring】04 注解实现自动装配

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

注解的基础源于JDK1.5的新特性

在Spring2.5开始支持了注解功能

如何使用?

1、导入约束

xmlns:context="http://www.springframework.org/schema/context"

1.5、约束坐标

http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd

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
       http://www.springframework.org/schema/beans/spring-beans.xsd
       
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
"
>
    <context:annotation-config />
</beans>

我们不再容器中编写繁琐的注入,直接在类中完成装配

    <bean id="cat33" class="cn.dai.pojo.Cat"/>
    <bean id="dog22" class="cn.dai.pojo.Dog"/>
    <bean id="person" class="cn.dai.pojo.Person"/>

@Autowired

既可以在字段上注解

也可以在SETTER上注解

这里使用Lombok,就没有办法在SETTER注解了。。。

Spring可以在没有SETTER方法的情况下,直接对字段进行装配

【Spring】04 注解实现自动装配

import org.springframework.lang.Nullable;

@Nullable 被注解的字段或者参数允许为空

//如果允许对象为null,设置required = false,默认为true
@Autowired(required = false)
private Cat cat;

@Qualifier

@Autowired是根据类型自动装配的,加上@Qualifier则可以根据byName的方式自动装配

@Qualifier不能单独使用。


测试实验步骤:

配置文件修改内容,保证类型存在对象。且名字不为类的默认名字!

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

没有加Qualifier测试,直接报错

在属性上添加Qualifier注解

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

测试,成功输出!

@Resource

1、@Resource如有指定的name属性,先按该属性进行byName方式查找装配;
2、其次再进行默认的byName方式进行装配;
3、如果以上都不成功,则按byType的方式自动装配。
4、都不成功,则报异常。

实体类:

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

配置文件2:beans.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异同:

@Autowired与@Resource都可以用来装配bean。

都可以写在字段上,或写在setter方法上。

@Autowired默认按类型装配(属于spring规范)

默认情况下必须要求依赖对象必须存在

如果要允许null 值,可以设置它的required属性为false,

如:@Autowired(required=false)

如果我们想使用名称装配可以结合@Qualifier注解进行使用

@Resource(属于J2EE规范)

默认按照名称进行装配,名称可以通过name属性进行指定。

如果没有指定name属性,当注解写在字段上时,默认取字段名进行按照名称查找,

如果注解写在setter方法上默认取属性名进行装配。

当找不到与名称匹配的bean时才按照类型进行装配。

但是需要注意的是,如果name属性一旦指定,就只会按照名称进行装配。

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

@Autowired先byType,@Resource先byName。

完整约束

<?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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"

       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
"
>
    <context:annotation-config />

</beans>