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. about the pageload and page init event

    Page_Init The Page_Init event is the first to occur when an ASP.NET page is executed. This is where ...

  2. 宝马测试(C++实现)

       测试目的:对编辑器放大,缩小性能测试.      测试资源:一匹宝马.       测试结果:良好. 实现方法:通过调用本地保存的宝马文件,逐字逐行的显示在编辑器中,并放大,缩小.对不同的符号进 ...

  3. 实习感悟——SQL语句

    在这次实习中用到了很多SQL语句,下面就给大家分享分享: 1.group by 字面意思我们一看就知道groupby通过分组的意思,通过数据库某个字段的分组我们可以做什么?联系到生活中,我们给一组对象 ...

  4. knockoutjs表格增加更新删除

    <!DOCTYPE html> <html> <head> <meta name="viewport" content="wid ...

  5. oracle 11g 分区表

    查看所有用户分区表及分区策略(1.2级分区表均包括): SELECT p.table_name AS 表名, decode(p.partitioning_key_count, 1, '主分区') AS ...

  6. 2天驾驭DIV+CSS (技巧篇)(转)

     这是去年看到的一片文章,感觉在我的学习中,有不少的影响.于是把它分享给想很快了解css的兄弟们.本文是技巧篇. 基础篇[知识一] “DIV+CSS” 的叫法是不准确的[知识二] “DIV+CSS” ...

  7. Go support for Android

    Go support for Android David Crawshaw June 2014 Abstract We propose to introduce Go support for the ...

  8. Go语言工程结构

    Go是一门推崇软件工程理念的编程语言. Go的代码必须放在GOPATH目录下,它应该包含三个子目录: src:用于以代码包的形式组织并保存Go源码文件.应该分为三类:库源码文件.命令源码文件.测试源码 ...

  9. Jsp实现筛选并压缩文件批量下载

    Jsp实现筛选并压缩文件批量下载 首先明确一下需求,网页端点击一下button,传递特定的参数到download.jsp网页,筛选文件,对过滤得到的文件进行压缩,然后返回前端一个压缩包下载. 以下的代 ...

  10. Hibernate从入门到精通(十一)多对多双向关联映射

    上次我们在中Hibernate从入门到精通(十)多对多单向关联映射讲解了一下多对多单向关联映射,这次我们讲解一下七种映射中的最后一种多对多双向关联映射. 多对多双向关联映射 按照我们之前的惯例,先看一 ...