程序笔记   发布时间:2022-07-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了spring-Bean的自动装配大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

1.手动装配,准备测试环境

1.1创建三个实体类

public class Dog {
    public void shout(){
        System.out.println("汪汪");
    }
}
public class Cat {
    public void shout(){
        System.out.println("喵喵");
    }
}

People 类 需要添加 get,set,toString方法

public class People {
    private String name;
    private Dog dog;
    private Cat cat;
}

1.2 编写spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/scheR_672_11845@a/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLscheR_672_11845@a-instance"
       xsi:scheR_672_11845@aLOCATIOn="http://www.springframework.org/scheR_672_11845@a/beans
        https://www.springframework.org/scheR_672_11845@a/beans/spring-beans.xsd">

    <bean id="cat" class="com.lv.pojo.Cat"/>
    <bean id="dog" class="com.lv.pojo.Dog"/>

    <bean id="people" class="com.lv.pojo.People">
        <property name="name" value="小王八"/>
        <property name="dog" ref="dog"/>
        <property name="cat" ref="cat"/>
    </bean>
</beans>

1.3 测试

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

1.4 执行结果

spring-Bean的自动装配

2 自动装配

2.1 使用配置文件

2.1.1 byName 按名称自动装配

修改配置文件,byName 会自动在容器上下文中查找,找自己对象set方法后面的值对应的Bean id

<bean id="cat" class="com.lv.pojo.Cat"/>
<bean id="dog1" class="com.lv.pojo.Dog"/>

<bean id="people" class="com.lv.pojo.People" autowire="byName">
    <property name="name" value="小王八"/>
</bean>

经过测试,成功输出,测试结果与上面相同

注意 : Bean 的 id 一定要和自己对象set方法名后面的值相同,例如setCat()方法,Bean的id需要写成 cat ,否则找不到会报空指针异常

2.1.2 byType 按类型自动装配

修改配置文件,byType 会自动在容器上下文中查找,和自己对象属性相同的Bean, 删除Bean的 id 也不会影响结果

<bean class="com.lv.pojo.Cat"/>
<bean class="com.lv.pojo.Dog"/>

<bean id="people" class="com.lv.pojo.People" autowire="byType">
    <property name="name" value="小王八"/>
</bean>

经过测试,成功输出,测试结果与上面相同

注意 : 这种方式不允许有class相同的Bean,因为根据类型查找,如果有类型相同的Bean,spring就无法确定我们需要的是哪一个了,会报NoUniqueBeanDefinitionException

2.2 使用注解 (推荐使用)

2.2.1 准备

2.2.1.1 在spring配置文件的文件头添加context约束

xmlns:context="http://www.springframework.org/scheR_672_11845@a/context"
http://www.springframework.org/scheR_672_11845@a/context
http://www.springframework.org/scheR_672_11845@a/context/spring-context.xsd

2.2.1.2 开启注解支持

<context:Annotation-config/>

2.2.1.3 修改后的spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/scheR_672_11845@a/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLscheR_672_11845@a-instance"
       xmlns:context="http://www.springframework.org/scheR_672_11845@a/context"
       xsi:scheR_672_11845@aLOCATIOn="http://www.springframework.org/scheR_672_11845@a/beans
        https://www.springframework.org/scheR_672_11845@a/beans/spring-beans.xsd
        http://www.springframework.org/scheR_672_11845@a/context
        http://www.springframework.org/scheR_672_11845@a/context/spring-context.xsd">
    
    <context:Annotation-config/>

    <bean id="cat555" class="com.lv.pojo.Cat"/>
    <bean id="dog" class="com.lv.pojo.Dog"/>
    <bean id="people" class="com.lv.pojo.People"/>
</beans>

2.2.2 @Autowired 

默认使用byType装配属性,若出现类型相同,则按照byName装配,若名字也匹配不到就报错BeanDefinitionParsingException

2.2.2.1使用方法 

在实体类中需要装配的属性上添加@Autowired注解,@Autowired注解装配属性不需要set方法,如果有set方法写在set方法上也可以

@Autowired
private Dog dog;
@Autowired
private Cat cat;

经过测试,成功输出,测试结果与上面相同

2.2.2.2 required参数

@Autowired 有一个参数required 默认是 true 代表该属性不能为NULL,必须存在对象,也就是说在某个属性上添加了 @Autowired 后 spring配置文件中就必须 配置出对应的 Bean 否则就报错,如果手动设置 rerequired = false 那么就允许这个属性为null了

@Autowired(required = falsE)
private Cat cat;

2.2.3 @Qualifier

通常与@Autowired联合使用,当有多个class类型重复的Bean时,可以给属性起名字,来控制@Autowired通过byName方式装配时,使用哪个Bean

2.2.3.1 修改 spring配置文件

<bean id="cat111" class="com.lv.pojo.Cat"/>
<bean id="cat222" class="com.lv.pojo.Cat"/>
<bean id="cat333" class="com.lv.pojo.Cat"/>

2.2.3.2 添加 @Qualifier 注解

@Autowired
@Qualifier(value = "cat222")
private Cat cat;

经过测试,成功输出,测试结果与上面相同

2.2.4 @resource

不是spring的注解,是java自带的

import javax.Annotation.resource;

@resource 是先通过 byName 的方式装配,如果没有找到id对应属性名的Bean,再通过 byType 的方式装配,如果发现了多个class类型重复的Bean,就报错NoUniqueBeanDefinitionException

2.2.4.1使用方法 

在实体类中需要装配的属性上添加@resource注解,@resource注解装配属性不需要set方法,如果有set方法写在set方法上也可以,如果不起名字使用默认的属性名,如果起名字 需要在@resource注解中跟一个name参数

2.2.4.1.1 添加注解

@resource(name = "dog111")
private Dog dog;

2.2.4.1.2 spring配置文件

<bean id="dog111" class="com.lv.pojo.Dog"/>
<bean id="dog222" class="com.lv.pojo.Dog"/>

经过测试,成功输出,测试结果与上面相同

大佬总结

以上是大佬教程为你收集整理的spring-Bean的自动装配全部内容,希望文章能够帮你解决spring-Bean的自动装配所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。