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中自动装配(转)的更多相关文章

  1. spring中自动装配bean

    首先用@Component注解类: package soundsystem: import org.springframework.stereotype.Component; @Component p ...

  2. Spring中自动装配的模式

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11484037.html 自动装配模式 Reference https://docs.spring.io ...

  3. 【面试普通人VS高手系列】Spring Boot中自动装配机制的原理

    最近一个粉丝说,他面试了4个公司,有三个公司问他:"Spring Boot 中自动装配机制的原理" 他回答了,感觉没回答错误,但是怎么就没给offer呢? 对于这个问题,看看普通人 ...

  4. Spring的自动装配Bean

    spring的自动装配功能的定义:无须在Spring配置文件中描述javaBean之间的依赖关系(如配置<property>.<constructor-arg>).IOC容器会 ...

  5. spring完成自动装配

    让spring完成自动装配 Autowiring 解决标签为javaBean注入时难以维护而实现的 下面是几种autowire type的说明: 1,byname:试图在容器中寻找和需要自动装配的属性 ...

  6. spring的自动装配,骚话@Autowired的底层工作原理

    前言 开心一刻 十年前,我:我交女票了,比我大两岁.妈:不行!赶紧分! 八年前,我:我交女票了,比我小两岁,外地的.妈:你就不能让我省点心? 五年前,我:我交女票了,市长的女儿.妈:别人还能看上你?分 ...

  7. Spring Boot 自动装配(二)

    目录 目录 前言 1.起源 2.Spring Boot 自动装配实现 2.1.@EnableAutoConfiguration 实现 2.1.1. 获取默认包扫描路径 2.1.2.获取自动装配的组件 ...

  8. 五、Spring之自动装配

    Spring之自动装配 ​ Spring利用依赖注入(DI),完成对IOC容器中各个组件依赖关系的赋值. [1]@Autowired @Autowired 注解,它可以对类成员变量.方法及构造函数进行 ...

  9. Eureka 系列(03)Spring Cloud 自动装配原理

    Eureka 系列(03)Spring Cloud 自动装配原理 [TOC] 0. Spring Cloud 系列目录 - Eureka 篇 本文主要是分析 Spring Cloud 是如何整合 Eu ...

随机推荐

  1. 2013-10-25笔记,css: mini-width, 标准居中,样式中*号使用,背景图像位置定位

    mini-width:设置元素的最小宽度.該屬性值會對元素的寬度設置一個最小限制.因此,元素可以比制定值寬,但不能比制定值窄.不允許指定負值. 完美的居中佈局: body{text-align: ce ...

  2. 通过Messenger与后台连接(单向操作,activity向service发送数据)

    xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:t ...

  3. jsonp 调用天气API

    由于Sencha Touch 2这种开发模式的特性,基本决定了它原生的数据交互行为几乎只能通过AJAX来实现. 当然了,通过调用强大的PhoneGap插件然后打包,你可以实现100%的Socket通讯 ...

  4. 生日蛋糕 (codevs 1710) 题解

    [问题描述] 7月17日是Mr.W的生日,ACM-THU为此要制作一个体积为Nπ的M层生日蛋糕,每层都是一个圆柱体. 设从下往上数第i(1<=i<=M)层蛋糕是半径为Ri,高度为Hi的圆柱 ...

  5. LPHW-积累-ex1-6

    Learn Python The Hard Way  ex0  介绍了各个操作系统下python的安装:强调了初学者最好使用简单的编辑器,不要使用IDE环境 ex1 使用 print 输出简单的字符串 ...

  6. char与 int 类型转化问题汇总

    1.char变为int时高位符号扩展问题 int main() { char a = 0x9a; int util; util = (int)a; if(util > 0) printf(&qu ...

  7. SOCKET 地址

    地址格式: 函数bind和getsockname使用通用数据类型:struct sockaddr*来指向socket地址. #incude <sys/socket.h> struct so ...

  8. struts2传递参数值的3中方式

    在使用struts2的时候,当要传递的参数不多的时候,我们会选择使用属性来传参,而当要传递的参数很多的时候,或者多个action会有共用的参数时,我们会使用另外两种传参方式. 注意:使用Model D ...

  9. 编写可维护的JavaScript之简易模版

    /* * 正则替换%s * @para arg1(text) 需要替换的模版 * @para arg2 替换第一处%s * @para arg3 替换第二处%s * 返回替换后的字符串 */ var ...

  10. 【linux命令系列】熟练运用每一个光标移动到最前和最后

    ctrl+e?a和e      ahead 和 end 看一个真正的专家操作命令行绝对是一种很好的体验-光标在单词之间来回穿梭,命令行不同的滚动.在这里强烈建立适应GUI节目的开发者尝试一下在提示符下 ...