spring——自动装配【非常详细】
什么是自动装配?
自动装配就是让应用程序上下文为你找出依赖项的过程。说的通俗一点,就是Spring会在上下文中自动查找,并自动给bean装配与其关联的属性!
spring中实现自动装配的方式有两种,一种是通过xml文件、另一种是通过注解。下面将为大家介绍这两种方式实现自动装配。
为了更简单的让大家理解,我们通过例子来说明:
有以下三个实体类,People类,Dog类,Cat类,分别代表人、狗、猫。人养了一只狗和一只猫,猫和狗都会叫。
public class Cat {
public void shout(){
System.out.println("miao~");
}
}
Cat类
public class Dog {
public void shout(){
System.out.println("wang wang~");
}
}
Dog类
public class Peopel {
private Cat cat;
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;
} @Override
public String toString() {
return "Peopel{" +
"cat=" + cat +
", dog=" + dog +
", name='" + name + '\'' +
'}';
}
}
people类
手动装配
讲自动装配之前,我们先来说一下手动装配,手动装配又是什么?手动装配就是手动的将bean中所关联的其他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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="cat" class="com.kuang.pojo.Cat"/>
<bean id="dog" class="com.kuang.pojo.Dog"/>
<bean id="people" class="com.kuang.pojo.Peopel">
<property name="name" value="张三"/>
<property name="cat" ref="cat"/>
<property name="dog" ref="dog"/>
</bean>
</beans>
在id=people的bean(以后id=xx的bean我们就叫xxBean)中,我们给peopleBean手动装配了与之关联的catBean和dogBean,这就叫做手动装配。
那么有没有什么办法,我们可以不用去手动装配关联的bean,让spring帮我们自动把关联的bean装配进去呢?答案是肯定的。自动装配就可以帮助我们解决这个问题。实现自动装配有两种方式。一种是使用注解的方式、另一种是通过xml文件的方式。下面我们俩讲实现自动装配的两种方式。
方式一:通过xml文件实现自动装配
我们只需要在xml配置文件中的bean标签中加入一个属性autowire即可,例如:
<bean id="people" class="com.kuang.pojo.Peopel" autowire="byName">
<property name="name" value="张三"/>
</bean>
使用autowire关键字声明bean的自动装配方式。其可选值为byName、byType、constructor,default,no;这里讲前边两个。
1.byName
设置autowire属性为byName,那么Spring会根据class属性找到实体类,然后查询实体类中所有setter方法的名字,根据setter方法后面的名字(例如SetDog,则setter方法后面的名字为dog)再到配置文件中寻找一个与该名字相同id的Bean,注入进来。如图:
2.byType
设置autowire属性为byType,那么Spring会自动寻找一个与该属性类型相同的Bean,注入进来。
*注意:使用byType这种方式,必须保证配置文件中所有bean的class属性的值是唯一的,否则就会报错
例如:下边这种方式是错误的,因为两个bean中的class属性的值重复了,会报错
方式二:通过注解实现自动装配
注解是通过反射来实现的。
1.使用注解前的准备:
要使用注解,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
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>
注意:
<conteext:annotation-config/> 必须要写在xml中,这是用来开启注解的支持,如果不加上注解就无效。
2.使用
2.1 Autowired注解【常用】
首先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
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/> <bean id="cat" class="com.kuang.pojo.Cat"/>
<bean id="dog" class="com.kuang.pojo.Dog"/>
<bean id="people" class="com.kuang.pojo.Peopel">
<property name="name" value="张三"/>
</bean>
</beans>
然后在实体类的对应属性上添加@Autowired注解(也可以把注解放到对应属性的setter上),people类中依赖Dog类和Cat类。所以在people类中的dog和cat属性上要加上@Autowired,实现自动装配。
例如:
public class Peopel {
@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;
}
@Override
public String toString() {
return "Peopel{" +
"cat=" + cat +
", dog=" + dog +
", name='" + name + '\'' +
'}';
}
}
**重点:
(1)注解方法装配属性的过程:spring会默认优先根据(被注解修饰的)属性类型去容器中找对应的组件(bean),找到就赋值;若找到多个相同类型的组件,再将属性的名称作为组件(bean)的id去容器中查找。
(2)@Qualifier注解可以和使用Autowired搭配使用:@Qualifier指定需要装配的组件的id,而不是使用属性名。例如下边例子,spring就会优先在容器中查找id为“abcd”的组件。
public class Peopel {
@Autowired
@Qualifier(value = "cat")
private Cat cat;
}
什么情况会使用到@Qualifier注解:当ioc容器根据属性类型去容器中找找到多个相同类型的组件,再将属性的名称作为组件(bean)的id去容器中查找找不到时就是用这两个注解搭配,指定需要装配的bean的id。
(3)在默认情况下使用 @Autowired 注释进行自动注入时,Spring 容器中匹配的候选 Bean 数目必须有且仅有一个。当找不到一个匹配的 Bean 时,Spring 容器将抛出 BeanCreationException 异常,并指出必须至少拥有一个匹配的 Bean。当不能确定 Spring 容器中一定拥有某个类的 Bean 时,可以在需要自动注入该类 Bean 的地方可以使用@Autowired(required= false)。这等于告诉 Spring:在找不到匹配 Bean 时也不报错。
2.2. Resource注解【不常用】
@Resource:可以和@Autowired一样实现自动装配功能,但是跟@Autowired不一样的是,它默认是按照组件名称进行装配的,按照组件名称找不到在根据属性类型去查找,再找不到就报错;他们另一个不同的地方就是@Autowired是Spring定义的; @Resource是java规范。
spring——自动装配【非常详细】的更多相关文章
- Spring 自动装配 Bean
Spring3系列8- Spring 自动装配 Bean 1. Auto-Wiring ‘no’ 2. Auto-Wiring ‘byName’ 3. Auto-Wiri ...
- spring 自动装配 default-autowire="byName/byType"
<PRE class=html name="code">spring 自动装配 default-autowire="byName/byType" ...
- Spring自动装配Bean详解
1. Auto-Wiring ‘no’ 2. Auto-Wiring ‘byName’ 3. Auto-Wiring ‘byType 4. Auto-Wirin ...
- Spring自动装配----注解装配----Spring自带的@Autowired注解
Spring自动装配----注解装配----Spring自带的@Autowired注解 父类 package cn.ychx; public interface Person { public voi ...
- Spring系列七:Spring 自动装配
相思相见知何日?此时此夜难为情. 概述 在Spring框架中,在配置文件中声明bean的依赖关系是一个很好的做法,因为Spring容器能够自动装配协作bean之间的关系.这称为spring自动装配. ...
- Spring自动装配(二)
为什么Spring要支持Autowire(自动装配) 先写几个类,首先定义一个Animal接口表示动物: 1 public interface Animal { 2 3 public void eat ...
- Spring自动装配歧义性笔记
Spring自动装配歧义性笔记 如果系统中存在两个都实现了同一接口的类,Spring在进行@Autowired自动装配的时候,会选择哪一个?如下: // 一下两个类均被标记为bean @Compone ...
- spring自动装配
spring提供了自动装配(autowiring)和自动检测(autodiscovery)用来减少XML的配置数量. 自动装配bean属性 byName——把与Bean的属性具有相同名字(或ID)的其 ...
- Spring自动装配与扫描注解
1 javabean的自动装配 自动注入,减少xml文件的配置信息. <?xml version="1.0" encoding="UTF-8"?> ...
随机推荐
- Python中的socket网络模块
目录 Socket 服务端(server.py) 客户端(client.py) socket中的一些常用方法 Socket 对象(内建)方法 Python Internet 模块 Python3 提供 ...
- Win64 驱动内核编程-1.环境搭建
驱动开发环境及其双机调试环境搭建 开发环境搭建 使用工具:vs2015,Windows 10 SDK_10.0.14393,WDK10.0.14393.0 (1)安装VS2015 随便一个版本吧,我 ...
- 初探 Git Submodules
之前一直想将一个 Git 仓库放到另一个 Git 仓库,有 Maven 多模块项目(Maven Multimodule Project)和 Gradle 多项目构建(Gradle Multiproje ...
- pr恢复工作区
当工作区操作的位置很乱时 平时如果关闭的窗口,可以在窗口中查看 也可以选择新建工作区,保存成一个自己所需工作区
- Java中如何保证线程顺序执行
只要了解过多线程,我们就知道线程开始的顺序跟执行的顺序是不一样的.如果只是创建三个线程然后执行,最后的执行顺序是不可预期的.这是因为在创建完线程之后,线程执行的开始时间取决于CPU何时分配时间片,线程 ...
- jQuery清空元素和克隆元素
1.清空 $(function () { $('#btn').click(function () { $('#ul1').html('') $('#ul1').empty() $('#ul1').re ...
- 烽火SATA SSD DSS200-B
烽火SATA SSD DSS200-B 运营商用户 > 产品与解决方案 > 产品 烽火SATA SSD DSS200-B 烽火通信 DSS200-B 2.5" SATA SSD ...
- 文件不同 diff --brief XX YY
文件不同 diff --brief XX YY 文件不同 --哪些位置 diff -c XX YY 9.diff命令 diff命令用于比较多个文本文件的差异,格式为"diff [参数] 文件 ...
- BUUCTF(八)[极客大挑战 2019]LoveSQL
BUUCTF 1.打开题目 注入方法可参考NewsCenter 2.测试注入点 username: 1'or'1=1 password: 1'or'1=1 登录成功,说明存在注入漏洞. 下面测试位点个 ...
- Windows 常用Cmd命令行 (持续更新...)
查看IP ipconfig 查看WIFI密码 netsh wlan show profiles wifi_name key = clear 系统探针 systeminfo CMD重定向 输出符号> ...