Spring中自动装配(转)
Spring中有四种自动装配类型,分别为:byName,byType,constructor,autodetect,下面来分别介绍一下这些是如何自动装配的
<bean id="foo" class="...Foo" autowire="autowire type">
有四种自动装配类型:
1.byName:寻找和属性名相同的bean,若找不到,则装不上。
2.byType:寻找和属性类型相同的bean,找不到,装不上,找到多个抛异常。
3.constructor:查找和bean的构造参数一致的一个或
多个bean,若找不到或找到多个,抛异常。按照参数的类型装配
4.autodetect: (3)和(2)之间选一个方式。不确定性的处理与(3)和(2)一致。
<bean id="bar" class="Bar" autowire="byName"/>
在介绍实例之前先要创建结构,我们以一个实例开始,用Customers来做实例,实例的结构为:
我们要创建一个CustomersServiceImpl.java,内容为:
package cn.csdn.hr.service;
import cn.csdn.hr.dao.BaseDao;
import cn.csdn.hr.dao.CustomersDao;
import cn.csdn.hr.dao.CustomersDaoImpl;
publicclass CustomersServiceImpl implements CustomersService {
private CustomersDao customersDao = new CustomersDaoImpl();
private BaseDao baseDao;
// set方法注入
publicvoid setCustomersDao(CustomersDao customersDao) {
this.customersDao = customersDao;
}
publicvoid setBaseDao(BaseDao baseDao) {
this.baseDao = baseDao;
}
public CustomersDao getCustomersDao() {
returncustomersDao;
}
public BaseDao getBaseDao() {
returnbaseDao;
}
}
在xml中对上面的两个属性进行注入
1.首先来介绍一下没有autowire的效果,
<?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-2.5.xsd">
<!-- bean的注入方式 ref属性 ref元素-->
<!-- autowire自动装配的类型 -->
<bean id="customersServiceImpl" class="cn.csdn.hr.service.CustomersServiceImpl">
<property name="customersDao">
<bean class="cn.csdn.hr.dao.CustomersDaoImpl"/>
</property>
<property name="baseDao">
<bean class="cn.csdn.hr.dao.BaseHibernateDaoImpl"/>
</property>
</bean>
</beans>
也可以用ref引用的方式来写,可以写为:
<?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-2.5.xsd">
<bean id="customersServiceImpl" class="cn.csdn.hr.service.CustomersServiceImpl">
<property name="customersDao">
<ref bean="customersDao"/>
</property>
<property name="baseDao">
<ref bean="baseDao"/>
</property>
</bean>
<bean id="customersDao" class="cn.csdn.hr.dao.CustomersDaoImpl"/>
<bean id="baseDao" class="cn.csdn.hr.dao.BaseHibernateDaoImpl"/>
</beans>
这样,在junit中测试为:
publicvoid test() {
//获取应用程序上下文对象
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:beanAuto.xml");
CustomersServiceImpl customersServiceImpl = (CustomersServiceImpl) ac.getBean("customersServiceImpl");
System.out.println("baseDao的实例"+customersServiceImpl.getBaseDao());
System.out.println("customersDao的实例"+customersServiceImpl.getCustomersDao());
}
我们可以得到:
baseDao的实例cn.csdn.hr.dao.BaseHibernateDaoImpl@12be1bd
customersDao的实例cn.csdn.hr.dao.CustomersDaoImpl@1f17e77
2.当把autowire设置为byName的时候,可以省略很多的代码,在junit和其他都不动的情况下,只改变xml,beanByName.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- autowire自动装配的类型
byName是根据名称自动装配
-->
<bean id="customersServiceImpl" class="cn.csdn.hr.service.CustomersServiceImpl" autowire="byName"/>
<bean id="customersDao" class="cn.csdn.hr.dao.CustomersDaoImpl"></bean>
<bean id="baseDao" class="cn.csdn.hr.dao.BaseHibernateDaoImpl"></bean>
</beans>
注:当autowire="byName"时,cn.csdn.hr.service.CustomersServiceImpl 属性名与bean的id名称的名称相同会自动装配。
.当把autowire设置为byType的时候
<?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-2.5.xsd">
<!-- autowire自动装配的类型
byType是根据类型自动装配 类型不能相同
cn.csdn.hr.service.CustomersServiceImpl 属性类型与bean中有相同的类型的时候会自动装配
-->
<bean id="customersServiceImpl" class="cn.csdn.hr.service.CustomersServiceImpl" autowire="byType"/>
<bean id="customersDaoImpl" class="cn.csdn.hr.dao.CustomersDaoImpl"></bean>
<bean id="baseDaoImpl" class="cn.csdn.hr.dao.BaseHibernateDaoImpl"></bean>
</beans>
注:不可以有相同的类型,也就是说不可以有相同的类名存在,id可有可无,但是一般情况下是存在的,它与其他的没有关联
4.当把autowire设置为constructor的时候
<?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-2.5.xsd">
<!-- autowire自动装配的类型
constructor是根据类型自动装配 根据构造器的参数来显示
cn.csdn.hr.service.CustomersServiceImpl 属性类型与bean中有相同的类型的时候会自动装配
-->
<bean id="customersServiceImpl" class="cn.csdn.hr.service.CustomersServiceImpl" autowire="constructor"/>
<bean id="customersDaoImpl" class="cn.csdn.hr.dao.CustomersDaoImpl"/>
<bean id="baseDaoImpl" class="cn.csdn.hr.dao.BaseHibernateDaoImpl"/>
</beans>
注:在执行这个xml的时候,要有构造函数,经过验证得出必须有有参构造才可以全部得到
5.当把autowire设置为autodetect的时候
<?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-2.5.xsd">
<!-- autowire自动装配的类型
cn.csdn.hr.service.CustomerServiceImpl
有没有默认的构造 没有就采用byType类型如果没有则采用constructor -->
<bean id="customersServiceImpl" class="cn.csdn.hr.service.CustomersServiceImpl" autowire="autodetect" />
<bean id="customersDaoImpl" class="cn.csdn.hr.dao.CustomersDaoImpl" />
<bean id="baseDaoImpl" class="cn.csdn.hr.dao.BaseHibernateDaoImpl" />
</beans>
Spring中自动装配(转)的更多相关文章
- spring中自动装配bean
首先用@Component注解类: package soundsystem: import org.springframework.stereotype.Component; @Component p ...
- Spring中自动装配的模式
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11484037.html 自动装配模式 Reference https://docs.spring.io ...
- 【面试普通人VS高手系列】Spring Boot中自动装配机制的原理
最近一个粉丝说,他面试了4个公司,有三个公司问他:"Spring Boot 中自动装配机制的原理" 他回答了,感觉没回答错误,但是怎么就没给offer呢? 对于这个问题,看看普通人 ...
- Spring的自动装配Bean
spring的自动装配功能的定义:无须在Spring配置文件中描述javaBean之间的依赖关系(如配置<property>.<constructor-arg>).IOC容器会 ...
- spring完成自动装配
让spring完成自动装配 Autowiring 解决标签为javaBean注入时难以维护而实现的 下面是几种autowire type的说明: 1,byname:试图在容器中寻找和需要自动装配的属性 ...
- spring的自动装配,骚话@Autowired的底层工作原理
前言 开心一刻 十年前,我:我交女票了,比我大两岁.妈:不行!赶紧分! 八年前,我:我交女票了,比我小两岁,外地的.妈:你就不能让我省点心? 五年前,我:我交女票了,市长的女儿.妈:别人还能看上你?分 ...
- Spring Boot 自动装配(二)
目录 目录 前言 1.起源 2.Spring Boot 自动装配实现 2.1.@EnableAutoConfiguration 实现 2.1.1. 获取默认包扫描路径 2.1.2.获取自动装配的组件 ...
- 五、Spring之自动装配
Spring之自动装配 Spring利用依赖注入(DI),完成对IOC容器中各个组件依赖关系的赋值. [1]@Autowired @Autowired 注解,它可以对类成员变量.方法及构造函数进行 ...
- Eureka 系列(03)Spring Cloud 自动装配原理
Eureka 系列(03)Spring Cloud 自动装配原理 [TOC] 0. Spring Cloud 系列目录 - Eureka 篇 本文主要是分析 Spring Cloud 是如何整合 Eu ...
随机推荐
- 在Nginx 下运行 Laravel5.1 的配置
一.nginx 的 vhost.conf 配置: server { listen ; server_name sub.domain.com; set $root_path '/srv/www/defa ...
- 有趣的EditView为空时的抖动效果(用户名和密码)--第三方开源--ClearEditText
ClearEditText在github上的链接地址是:https://github.com/zhangphil/ClearEditText 用法十分简单,在布局中使用ClearEditText,在J ...
- asp.net mvc razor html encoding
HTML Encoding 为了跨站点的脚本攻击,Razor 语法会直接将脚本代码编码输出. @{string message = "<script>alert('haacked ...
- SPARK 数据统计程序性能优化。
昨天写完R脚本 没测试就发到博客里, 结果实际运行发现很慢,运行时间在2小时以上, 查看spark控制台, 大量时间消耗在count上, 产生的stage多大70多个 . 分析原因. 1 selec ...
- 简单翻译和补充:1. GNU ARM Eclipse
原文链接: GNU ARM Eclipse GNU 介绍: GNU 计划,又称革奴计划,是由RichardStallman在1983年9月27日公开发起的.它的目标是创建一套完全自由的操作系统.Ric ...
- TeXworks代码补全功能
使用Tab键会自动补全(实在是太TM的方便了) 比如:
- C语言接口的写法(以toyls命令为例)
#include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h&g ...
- TAT 前端突击队 第四关 题目 腐蚀的画
腐蚀的画 1.一个漂亮的画作在经过几千年岁月的洗礼下,部分地方已经被腐蚀了,像一个孤独的老人,满脸爬满了皱纹.2.但在一个晚上,老王突然发现,这些腐蚀的部分中,隐藏着岁月留下的密秘.请你帮助老王寻找这 ...
- 在WIN7下安装运行mongodb 1)、下载MongoDB
1).下载MongoDB http://downloads.mongodb.org/win32/mongodb-win32-i386-2.4.5.zip 下载Windows 32-bit版本并解压缩, ...
- 硬件相关-ADC原理(未完成)
一.模数转换的一般步骤: 1)采样和保持 为了把模拟信号转换成对应的数字信号,必须首先将模拟量每隔一定时间抽取一次样值,使时间上连续变化的模拟量变为一个时间上断续变化的模拟量,这个过程称为采样. 为了 ...