5、Bean的自动装配

本文详细介绍了Spring框架中的自动装配机制,包括XML显式配置、Java显式配置、隐式bean发现(@Autowired)、byName和byType的区别,以及使用@Component注解和@Resource注解的装配方式。
摘要由CSDN通过智能技术生成

自动装配说明
● 自动装配是使用spring满足bean依赖的一种方法
● spring会在应用上下文中为某个bean寻找其依赖的bean。
Spring中bean有三种装配机制,分别是:

  1. 在xml中显式配置;
  2. 在java中显式配置;
  3. 隐式的bean发现机制和自动装配。【重要】
    autowire byName (按名称自动装配)
    autowire="byName"会自动在容器上下文查找,和自己set方法后面的值对应 的bean id
<bean id="cat" class="cn.zhy.Cat"/>
<bean id="dog" class="cn.zhy.Dog"/>


<bean id="people" class="cn.zhy.People" autowire="byName">
</bean>

小结
当一个bean节点带有 autowire byName的属性时。

将查找其类中所有的set方法名,例如setCat,获得将set去掉并且首字母小写的字符串,即cat。

去spring容器中寻找是否有此字符串名称id的对象。

如果有,就取出注入;如果没有,就报空指针异常。
autowire byType (按类型自动装配)
使用autowire byType首先需要保证:同一类型的对象,在spring容器中唯一。如果不唯一,会报不唯一的异常。NoUniqueBeanDefinitionException,一个接口如果有两个实现类会报错。
autowire="byType"会自动在容器上下文查找,和自己对象类型相同的bean

<bean class="cn.zhy.Cat"/>
<bean class="cn.zhy.Dog"/>

<bean id="people" class="cn.zhy.People" autowire="byType">
</bean>

使用注解实现自动装配
1、导入约束

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

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

2、配置注解支持 context:annotation-config/
xml配置

<?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">
    <!--xmlns:context="http://www.springframework.org/schema/context" 注意别导错了-->
    <!--开启组件扫描功能 扫描com.liu包下所有类是否添加了@Component注解-->
    <context:component-scan base-package="cn.zhy"/>
    <context:annotation-config/>
    <bean id="dog" class="cn.zhy.Dog"/>
    <bean id="cat" class="cn.zhy.Cat"/>
    <bean id="people" class="cn.zhy.People"/>
</beans>

3、导入依赖

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.3.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>4.3.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.3.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-expression</artifactId>
        <version>4.3.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>4.3.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>4.3.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>4.3.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>4.3.2.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.24</version>
    </dependency>
</dependencies>

@Autowired
直接在属性上使用即可!也可以在set方式上使用!
使用Autowired我们可以不用编写Set方法了,前提是你这个自动装配的属性在IOC(Spring)容器中存在,且符合名字byname!

@Autowired
private Cat cat;
@Autowired
private Dog dog;
<bean id="dog" class="cn.zhy.Dog"/>
    <bean id="cat" class="cn.zhy.Cat"/>
    <bean id="people" class="cn.zhy.People"/>

● 补充
○ @Nullable,字段标记了这个注解,说明这个字段可以位null
○ @Autowired(required=false) 说明:false,对象可以为null;true,对象必须存对象,不能为null。
@Qualifier
● @Autowired是根据类型自动装配的,加上@Qualifier则可以根据byName的方式自动装配
● @Qualifier不能单独使用。

@Autowired
@Qualifier(value = "cat111")
private Cat cat;
@Autowired
private Dog dog;
<bean id="dog" class="cn.zhy.Dog"/>
<bean id="cat" class="cn.zhy.Cat"/>
<bean id="cat111" class="cn.zhy.Cat"/>
<bean id="people" class="cn.zhy.People"/>

@Resource注解

@Resource
private Cat cat;
@Resource
private Dog dog;
<bean id="dog" class="cn.zhy.Dog"/>
<bean id="cat" class="cn.zhy.Cat"/>
<bean id="cat111" class="cn.zhy.Cat"/>
<bean id="people" class="cn.zhy.People"/>

小结:
Resource和@ Autowired的区别:
·都是用来自动装配的,都可以放在属性字段上
.@Autowired通过byType的方式实现,而且必须要求这个对象存在!【常用】
@Resource默认通过byname的方式实现,如果找不到名字,则通过byType实现!如果两个都找不到的情况下,就报错!【常用】
·执行顺序不同:@Autowired通过byType的方式实现。@Resource默认通过byname的方式实现。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值