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名称的名称相同会自动装配。

3.当把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自动装配

    spring Bean自动装配 自动装配是使用spring满足bean依赖的一种方式. spring会在应用上下文中为某个bean寻找其依赖的bean. spring自动装配需要从两个角度来实现,或者 ...

  2. (Spring)自动装配bean

    文章目录 自动装配bean 1. 环境搭建 2. byName自动装配 3. byType自动装配 4. 使用注解自动装配 4.1 @Autowired和@Qualifier 4.2 @Resourc ...

  3. Spring autowire 自动装配简介

    Spring autowire 自动装配简介 注意本文与一般spring 标注@Autowire 无关 如下例子定义了3个类 Dperson Dcar & Daddress 其中Dperson ...

  4. Spring Autowire自动装配(转http://lep1985520.blog.163.com/blog/static/56600480200901441338486/)

    Spring Autowire自动装配 技术收藏 2009-01-14 16:13:38 阅读284 评论0   字号:大中小 订阅 Spring Autowire自动装配   在应用中,我们常常使用 ...

  5. Spring学习——自动装配

    自动装配说明 自动装配是使用spring满足bean依赖的一种方法 spring会在应用上下文中为某个bean寻找其依赖的bean. Spring中bean有三种装配机制,分别是: 在xml中显式配置 ...

  6. Spring Boot自动装配过程解析及简单Demo演示

    文章目录 1.约定大于配置 2.自动装配原理 2.1.`@SpringBootApplication` 2.2.`@EnableAutoConfiguration` 2.3.`@Import` 2.4 ...

  7. Spring | Bean自动装配详解

    个人主页:BoBooY的CSDN博客_Java领域博主 前言:上节我给大家讲解了Spring的依赖注入,这一节我们讲解Spring中Bean如何自动装配,废话不多说,直接上正文! 文章目录 Bean的 ...

  8. Spring Boot自动装配原理详解

    目录 1.环境和依赖 1.1.spring boot版本 1.2.依赖管理 2.自动装配 2.1.流程概述 2.2.三大步前的准备工作 2.2.1.注解入口 2.2.2.获取所有配置类 2.3.获取过 ...

  9. Spring Autowire自动装配

    1.在应用中,我们常常使用<ref>标签为JavaBean注入它依赖的对象,同时也Spring为我们提供了一个自动装配的机制,在定义Bean时,<bean>标签有一个autow ...

  10. 【Spring】—— 自动装配

    一.Spring中装配bean的方式 1.在XML中显式配置 2.在Java中进行显式配置 3.隐士的bean发现机制和自动装配 二.自动装配示例 1.在需要装配到其他bean中的类中加入@Compo ...

最新文章

  1. 合肥工业大学—SQL Server数据库实验十:用户及其权限管理
  2. Tableau 必知必会之使用环境的配置需求
  3. 验证视图状态 MAC 失败的解决办法
  4. linux php 依赖,在php5-fpm-alpine docker容器中安装php-gd依赖于alpine linux
  5. Anton and Fairy Tale CodeForces - 785C(二分+思维)
  6. html中optgroup 标签用法,分组、只读的且呈树状样式显示
  7. python实现TCP远程服务器文件上传,下载系统
  8. RESTful开发风格
  9. 土耳其电信与华为签署5G协议谅解备忘录
  10. ModelForm views.py
  11. android dialogfragment 简单,使用DialogFragment
  12. JDK源码阅读-CharSequence接口
  13. 佳铁精雕机连接电脑设置_佳铁精雕机在程式里怎么更改G57之后的坐标
  14. Idea利用JSP模板设置统一路径(basePath)
  15. zblog模板 php,卢松松博客模板zblogphp版 适合seo功能强大(已支持1.6)
  16. Unity 特效 之 武器拖尾效果
  17. FxCAD实验一 简单图形的绘制
  18. 世界杯2022赛程表
  19. 挚爱家乡五常大米的味道
  20. GRU和LSTM的单元结构

热门文章

  1. 文本意图识别方案整理
  2. 2018-09-03 KK日记,记一次JVM内存使用过多的诊断
  3. JDK里的“SPI”是什么意思
  4. 未来的全能保姆机器人作文_保姆机器人作文300字共5篇
  5. FineBI学习笔记
  6. mysql慕课版_MySQL数据库管理与开发(慕课版)
  7. 事业单位计算机面试基础知识,求事业单位面试题目(计算机岗位)
  8. 【C语言】合并两个数组,降序排列并删除重复元素(通俗易懂)
  9. 2015-4-20分享的pdf
  10. mysql连接oracle视图_oracle数据库视图