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. Oracle中Clob类型处理解析:ORA-01461:仅可以插入LONG列的LONG值赋值

    感谢原作者:破剑冰-Oracle中Clob类型处理解析 上一篇分析:ORA-01461: 仅能绑定要插入 LONG 列的 LONG 值 最近为Clob字段在插入数据时发现当字符的字节数(一个半角字符一 ...

  2. 不复杂的Autofac注入

    private static void SetAutofacWebAPI() { var builder = new ContainerBuilder(); #region 配置注册方法 string ...

  3. 动态加载故事storyboard

    第一种方法: ViewControllerB *vcB = [self.storyboard instantiateViewControllerWithIdentifier:@"Name o ...

  4. 多点触摸画板(MultiTouchCanvas)

    这是个简单的支持多点触摸的画板控件, 绘制功能基于WPF InkCanvas,也是我drawTool系列文章的开篇. 阅读该文章后可能产生一些问题: 1. 如果对生成的笔迹对象进行控制 如果要对生成的 ...

  5. EMVTag系列11《电子现金发卡行授权码》

    按照银联个人化模板的建议,如卡片支持非接触快速支付应用(qPBOC),则推荐将电子现金授权码(9F74)作为qPBOC 应用AFL列表中的最后一条记录,且最后一条记录仅包含该数据元. 原因是:在某些情 ...

  6. EXCLE中快速插入图片

    在excle中怎么快速插入图片呢,一张一张点实在比较麻烦 解决办法: <table><img src="D:\A.png" width="60" ...

  7. Git 设置别名[alias]

    工作中我经常设置一下别名... 别名就在[alias]后面,要删除别名,直接把对应的行删掉即可. 而当前用户的Git配置文件放在用户主目录下的一个隐藏文件.gitconfig中: $ cat .git ...

  8. Media Player(APP)

    今天共享一下,以前做的影音播放器.比较简单.适合新手. 我上传了附件可以参考一下. PDF:http://files.cnblogs.com/files/hongguang-kim/MediaPlay ...

  9. hdu 1316 How Many Fibs?

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1316 How Many Fibs? Description Recall the definition ...

  10. VHDL----基础知识1

    摘要: 打算分几篇,来理清VHDL的基础知识 ----------------------------------------------------------------------------- ...