java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Spring Bean自动装配

Spring使用注解实现Bean的自动装配

作者:冷丁_

大家好,本篇文章主要讲的是Spring使用注解实现Bean的自动装配,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下

一、利用注解方式注入属性

在这里插入图片描述

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

如果在已有的配置文件中附加

在spring配置文件中引入context文件头

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

开启属性注解支持!

<context:annotation-config/>

二、@Autowired

@Autowired是按类型自动装配的,不支持id匹配demo测试: 将User类中的set方法去掉,使用@Autowired注解

public class People {
@Autowired
private Cat cat;
@Autowired
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;
}

此时配置文件内容

<context:annotation-config/>
<bean id="cat" class="com.lding.pojo.Cat"></bean>
<bean id="dog" class="com.lding.pojo.Dog"></bean>
<bean id="people" class="com.lding.pojo.People"></bean>

测试成功

@Autowired(required=false) false,对象可以为null;true,对象必须存对象,不能为null。

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

三、@Qualifier

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

@Qualifier不能单独使用。

demo测试

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

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

没有加Qualifier测试,直接报错在属性上添加Qualifier注解

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

测试 ,成功输出

四、@Resource

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

其次再进行默认的byName方式进行装配(默认的byName方式就是查找set后属性的小写名字);

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

都不成功,则报异常。

demo测试
实体类

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.lding.pojo.Dog"/>
<bean id="cat1" class="com.lding.pojo.Cat"/>
<bean id="cat2" class="com.lding.pojo.Cat"/>
<bean id="user" class="com.lding.pojo.User"/>

测试:结果ok
配置文件2:beans.xml , 删掉cat2

<bean id="dog" class="com.lding.pojo.Dog"/>
<bean id="cat1" class="com.lding.pojo.Cat"/>
@Resource
private Cat cat;
@Resource
private Dog dog;

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

总结

@Autowired与@Resource异同:

@Autowired与@Resource都可以用来装配bean。都可以写在字段上,或写在setter方法上。
@Autowired默认按类型装配(byType),如果我们想使用名称装配(byName)可以结合@Qualifier注解进行使用
@Resource(属于J2EE复返),默认按照名称进行装配,名称可以通过name属性进行指定。如果没有指定name属性,当注解写在字段上时,默认取字段名进行按照名称查找,如果注解写在setter方法上默认取属性名进行装配。 当找不到与名称匹配的bean时才按照类型进行装配。但是需要注意的是,如果name属性一旦指定,就只会按照名称进行装配。
它们的作用相同都是用注解方式注入对象,但执行顺序不同。@Autowired先byType,@Resource先byName。

到此这篇关于Spring使用注解实现Bean的自动装配的文章就介绍到这了,更多相关Spring Bean自动装配内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文