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 ...
随机推荐
- Hadoop、Yarn和vcpu资源的配置
转载自:https://www.cnblogs.com/S-tec-songjian/p/5740691.html Hadoop YARN同时支持内存和CPU两种资源的调度(默认只支持内存,如果想进 ...
- 在win10环境中安装xilinx vivado IDE时出现的问题及解决方法
1.问题:There is no valid Xilinx installation that this Update can be applied to. 解决方法一:下载的是更新包,如果设备没有预 ...
- 20155325 Exp9 Web安全基础
本实践的目标理解常用网络攻击技术的基本原理.Webgoat实践下相关实验. 实验后回答问题 (1)SQL注入攻击原理,如何防御 原理:SQL注入即是指web应用程序对用户输入数据的合法性没有判断,攻击 ...
- cocos2d-x学习记录1——图片显示
这篇算是cocos2d-x入门篇,显示一张图片即可. 观察工程中HelloWorld的结构,包含AppDelegate和HelloWorldScene两个类文件,AppDelegate中包含基本的处理 ...
- 使用DOS工具修复数据库
当SQL Server 实例出现异常,无法远程链接时,数据库管理员需要登陆到SQL Server实例机器上,通过命令行工具,修复异常. 一,使用net命令行启动数据库 通过net start 命令启动 ...
- SQL调优日志--内存问题排查入门篇
概述 很多系统的性能问题,是由内存导致的.内存不够会导致页面频繁换入换出,IO队列高,进而影响数据库整体性能. 排查 内存对数据库性能非常重要.那么我当出现问题的时候,我们怎么排查性能问题呢? 存在问 ...
- 9、Dockerfile实战-Nginx
上一节我们详解Dockerfile之后,现在来进行实战.我们通过docker build来进行镜像制作. build有如下选项: [root@localhost ~a]# docker build - ...
- .Net Core 分布式微服务框架 - Jimu 添加 Swagger 支持
系列文章 .Net Core 分布式微服务框架介绍 - Jimu .Net Core 分布式微服务框架 - Jimu 添加 Swagger 支持 一.前言 最近有空就优化 Jimu (一个基于.Net ...
- Centos 7 部署Kubernetes(K8S)集群
资源链接:https://pan.baidu.com/s/1-PT_QQAf7cTu_znX-S-r9Q 密码:33sr 转发:http://blog.51cto.com/lizhenliang/19 ...
- Java内存区域的划分和异常
Java内存区域的划分和异常 运行时数据区域 JVM在运行Java程序时候会将内存划分为若干个不同的数据区域. 打开百度App,看更多美图 程序计数器 线程私有.可看作是当前线程所执行的字节码的行 ...