您的位置:首页 > 编程语言 > Java开发

Spring自动装配

2015-09-19 17:00 441 查看
注:转载之>>>http://blog.csdn.net/wangli325/article/details/7471327

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

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: