ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

spring-Bean的自动装配

2022-01-17 10:33:00  阅读:104  来源: 互联网

标签:装配 配置文件 Autowired spring Bean 2.2


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/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/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 执行结果

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/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/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/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
        http://www.springframework.org/schema/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"/>

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

2.2.5 补充

  • @NonNull 可以标注在方法、字段、参数之上,表示对应的值不可以为空
  • @Nullable 注解可以标注在方法、字段、参数之上,表示对应的值可以为空

标签:装配,配置文件,Autowired,spring,Bean,2.2
来源: https://www.cnblogs.com/lv1024/p/15812085.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有