Spring点滴十:Spring自动装配(Autowire)
在基于XML配置元数据,在bean的配置信息中我们可以使用<constructor-arg/>和<property/>属性来实现Spring的依赖注入。Spring 容器也可以在不使用<constructor-arg/>和<property/>元素下自动装配各个bean之间的依赖关系。
自动装配模式:
在基于XML配置元数据时,你可以使用bean元素的autowire属性来指定具体的自动装配模式。Spring提供了5中自动装配模式。
模式 | 讲解 |
no | 这是默认设置,意味着它没有使用自动装配模式。你应该显示的使用bean引用来连接 |
byName | 由属性名自动装配。Spring 容器看到bean采用了自动装配byName模式(autowire="byName"),然后根据它的属性在Spring 容器中寻找与属性名相同bean进行关联 |
byType | 由属性的数据类型自动装配。Spring容器看到bean采用了自动装配的byType模式(autowire="byType"),然后根据属性类型在Spring容器中寻找与属性类型相同bean进行关联。如果存在不止一个这样的bean,将抛出异常。 |
constructor | 类似于 byType,但该类型适用于构造函数参数类型。如果在容器中没有一个构造函数参数类型的 bean,则一个致命错误将会发生 |
autodetect | Spring首先尝试通过 constructor 使用自动装配来连接,如果它不执行,Spring 尝试通过 byType 来自动装配 |
byName示例:
<?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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="autowireTest1" class="com.test.spring.AutowireTest1" autowire="byName">
</bean>
<bean id="autowireTest2" class="com.test.spring.AutowireTest2">
</bean>
</beans>
Java 类:
package com.test.spring; public class AutowireTest1 { private AutowireTest2 autowireTest2; public AutowireTest2 getAutowireTest2() {
return autowireTest2;
} public void setAutowireTest2(AutowireTest2 autowireTest2) {
this.autowireTest2 = autowireTest2;
}
}
-------------------------------------------------------------------------------------------
package com.test.spring; public class AutowireTest2 { }
测试:
package com.test.spring; import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class T {
ApplicationContext applicationcontext=null;
@Before
public void before() {
System.out.println("》》》Spring ApplicationContext容器开始初始化了......");
applicationcontext= new ClassPathXmlApplicationContext(new String[]{"test1-service.xml"});
System.out.println("》》》Spring ApplicationContext容器初始化完毕了......");
}
@Test
public void test() {
AutowireTest1 autowireTest1= applicationcontext.getBean(AutowireTest1.class);
System.out.println(autowireTest1.getAutowireTest2());
}
}
测试结果:
》》》Spring ApplicationContext容器开始初始化了......
2017-03-19 22:53:54 INFO:ClassPathXmlApplicationContext-Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@496419dc: startup date [Sun Mar 19 22:53:54 CST 2017]; root of context hierarchy
2017-03-19 22:53:54 INFO:XmlBeanDefinitionReader-Loading XML bean definitions from class path resource [test1-service.xml]
》》》Spring ApplicationContext容器初始化完毕了......
com.test.spring.AutowireTest2@63465272
byType:
只需要将bean改为如下: <bean id="autowireTest1" class="com.test.spring.AutowireTest1" autowire="byType"> 测试结果: 》》》Spring ApplicationContext容器开始初始化了......
2017-03-19 22:53:54
INFO:ClassPathXmlApplicationContext-Refreshing
org.springframework.context.support.ClassPathXmlApplicationContext@496419dc:
startup date [Sun Mar 19 22:53:54 CST 2017]; root of context hierarchy
2017-03-19 22:53:54 INFO:XmlBeanDefinitionReader-Loading XML bean definitions from class path resource [test1-service.xml]
》》》Spring ApplicationContext容器初始化完毕了......
com.test.spring.AutowireTest2@63465272 constructor: 只需要将bean改为如下: <bean id="autowireTest1" class="com.test.spring.AutowireTest1" autowire="constructor">
对应AutowireTest1类改为:
package com.test.spring; public class AutowireTest1 { private AutowireTest2 autowireTest2; public AutowireTest1(AutowireTest2 autowireTest2) {
this.autowireTest2 = autowireTest2;
}
}
测试结果输出:
》》》Spring ApplicationContext容器开始初始化了......
2017-03-20 09:13:48 INFO:ClassPathXmlApplicationContext-Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4752dd7b: startup date [Mon Mar 20 09:13:48 CST 2017]; root of context hierarchy
2017-03-20 09:13:48 INFO:XmlBeanDefinitionReader-Loading XML bean definitions from class path resource [test1-service.xml]
》》》Spring ApplicationContext容器初始化完毕了......
com.test.spring.AutowireTest1@43bb703d
总结:
自动装配的好处:
自动装配可以有效减少bean标签的<constructor-arg/>或<property/>元素的使用
自动装配的局限性和不利条件:
1.显示的依赖注入<constructor-arg/>或<property/>会重写自动装配。不能自动装配基本数据类型、字符串、数组等,这是自动装配设计的局限性。
2.自动装配不如显示依赖注入精确。如果有可能尽量使用显示依赖注入。
Spring点滴十:Spring自动装配(Autowire)的更多相关文章
- Spring学习记录(三)---bean自动装配autowire
Spring IoC容器可以自动装配(autowire)相互协作bean之间的关联关系,少写几个ref autowire: no ---默认情况,不自动装配,通过ref手动引用 byName---根据 ...
- [原创]java WEB学习笔记99:Spring学习---Spring Bean配置:自动装配,配置bean之间的关系(继承/依赖),bean的作用域(singleton,prototype,web环境作用域),使用外部属性文件
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Spring学习(六)-----Spring使用@Autowired注解自动装配
Spring使用@Autowired注解自动装配 在上一篇 Spring学习(三)-----Spring自动装配Beans示例中,它会匹配当前Spring容器任何bean的属性自动装配.在大多数情况下 ...
- spring(4)——自动装配
set注入和构造注入有时在做配置时比较麻烦.所以框架为了提高开发效率,提供自动装配功能,简化配置.spring框架式默认不支持自动装配的,要想使用自动装配需要修改spring配置文件中<bean ...
- 8 -- 深入使用Spring -- 7...4 使用自动装配
8.7.4 使用自动装配 在自动装配策略下,Action还是由Spring插件创建,Spring 插件在创建Action实例时,利用Spring的自动装配策略,将对应的业务逻辑组件注入Action实例 ...
- 在Spring中通过构造自动装配--constructor
在Spring中,可以使用“通过构造自动装配”,实际上是按构造函数的参数类型自动装配. 这意味着,如果一个bean的数据类型与其他bean的构造器参数的数据类型是相同的,那么将自动装配. packag ...
- 【Spring实战】—— 8 自动装配
本篇介绍一下自动装配的知识,Spring为了简化配置文件的编写.采用自动装配方式,自动的装载需要的bean. 自动装配 有以下几种方式: 1 byName 通过id的名字与属性的名字进行判断,要保证B ...
- Spring中Beans的自动装配概述
以下内容引用自http://wiki.jikexueyuan.com/project/spring/beans-autowiring.html: 在之前的做法上会参照这样的顺序:1.使用<bea ...
- spring《四》自动装配
byName模式<bean autowire="byName"> Spring会查找一个叫做date的bean定义. byType模式<bean autowire ...
随机推荐
- 从源代码解释Android事件分发机制
在ViewRootImpl的setView方法中.用户的触摸按键消息是体如今窗体上的.而windowManagerService则是管理这些窗体,它一旦接收到用户对窗体的一些触摸按键消息,会进行对应的 ...
- 【转】Google Chrome中顺时针/逆时针滚动圆的含义
当浏览器处于以下状态时,看起来好像圆圈是逆时针滚动的: 解析主机名 连接服务器 等待响应(从服务器发送第一个字节之前?) 当浏览器处于以下状态时,圆圈似乎顺时针滚动: 加载页面或引用的资源 在标签页中 ...
- kettle学习笔记(八)——kettle查询步骤与连接步骤
一.概述 查询步骤: 用来查询数据源里的数据并合并到主数据流中 . 连接步骤: 结果集通过关键字进行连接 .(与前面的UNION不同) 二.查询步骤 1.流查询 流查询示例:(注意上文中的流查询的限制 ...
- 20155202张旭 Exp5 MSF基础应用
20155202张旭 Exp5 MSF基础应用 实践内容 本次实验我使用的攻击方式: 1.针对office软件的主动攻击:--MS10-087: 2.MS10-002漏洞对浏览器攻击 3.针对客户端的 ...
- 20155211《网络对抗》Exp02 后门原理与实践
20155211<网络对抗>Exp02 后门原理与实践 实验内容 (1)使用netcat获取主机操作Shell,cron启动 (2)使用socat获取主机操作Shell, 任务计划启动 ( ...
- TMS320VC5509的MCBSP配置成SPI模式通信
1. 首先是把MCBSP的配置 其次是时钟停止模式的配置,关闭大同小异 SPI有4中模式,怎么根据上面的寄存器选择哪种模式?下面展示了其中两种,CLKXP=1的时候有另外两种,暂时不整出来了 2. 代 ...
- mpvue两小时,产出一个《点钞辅助工具》小程序
CoffeeScript,Pug,Sass使用 以下内容门槛较高,如看不懂或觉得需要继续了解,结尾处放置了原视频流程与GitHub地址,欢迎琢磨与Star,谢谢. 文章不做技术语法解读,如不清楚,请前 ...
- 浅谈android Service和BroadCastReceiver
1.题记 Android中的服务和windows中的服务是类似的东西,服务一般没有用户操作界面,它运行于系统中不容易被用户发觉,可以使用它开发如监控之类的程序. 广播接收者(BroadcastRece ...
- Harbor私有镜像仓库无坑搭建
转载:https://k8s.abcdocker.com/kubernetes_harbor.html 一.介绍 Docker容器应用的开发和运行路不开可靠的镜像管理,虽然Docker官方也提供了公共 ...
- RabbitMQ使用注意
1.通常发布者发布结束后会释放Channel,但是消费者由于是异步监听,消费者的Channel不可以释放,否则就断开连接无法监听. 2.当使用默认配置时,ConnectionFactory不指定Por ...