- 浏览: 92155 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (133)
- jQuery (11)
- XML (3)
- 组件 (1)
- JAVA (20)
- WEB (3)
- SPRING (6)
- HIBERNATE (5)
- AJAX (2)
- JS (1)
- JAVA webservice (1)
- Ditu (1)
- WEBSITE (1)
- HIBERNATE ANNOTATION (1)
- 排序 (1)
- TCP_NODELAY (1)
- ConvertUtils (1)
- Logistics (1)
- SQL SERVER 中identity (4)
- sql server (35)
- MYSQL (1)
- Eclipse (6)
- ORACLE (6)
- FLEX (4)
- notepad++ (0)
- UNION ALL (1)
- JUnit (3)
- SQL 异常处理 (1)
- @@trancount (1)
- IOS (1)
- ORA-02266 (1)
- REMOTE DESKTOP (0)
- HTML 优化 (1)
- CRLF (1)
- SQL Server Sequence (1)
最新评论
-
zjuttsw:
看的舒服
重要的hashcode equals转载
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>
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>
发表评论
-
ContextLoaderPlugIn方式整合Struts和Spring
2013-04-05 19:06 602http://blog.sina.com.cn/s/blog_ ... -
BeanCopier与BeanUtil比较
2013-04-03 09:16 1163copy bean 有三种方式: 1. 使用BeanUtil ... -
spring获取webapplicationcontext,applicationcontext几种方法详解
2013-04-02 23:47 599http://www.blogjava.net/Todd/ar ... -
Spring Aop 拦截 Struts2 Action 有关问题
2013-03-03 11:21 573http://www.myexception.cn/softw ... -
转载SPRING源代码解析
2012-12-20 17:24 511http://www.iteye.com/topic/1123 ...
相关推荐
本项目为Java项目,使用Spring实现自动装配,根据类型方式实现自动装配。适合初学者了解Spring不同装配方式。
Spring自动装配解析,具体效果和过程看博文 http://blog.csdn.net/evankaka/article/details/44836069
spring的自动装配
今天小编就为大家分享一篇关于Spring中自动装配的4种方式,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
ean的自动装配,有4种 (1)no:不做任何操作 (2)byName:根据属性 名 自动装配,设值注入 <bean id="xxx" class="xxx" ></bean> (3)byType:根据属性 类型 自动装配,相同类型多个会抛出异常,设值注入 ...
Spring中的自动装配有哪些限制?.docx Spring中的自动装配有哪些限制?.docx Spring中的自动装配有哪些限制?.docx
今天小编就为大家分享一篇关于在Spring中自动装配Bean的属性,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
spring自动装配项目struts2,使用了struts2 ,spring2.5 ,hibernate,和spring 自动装配
spring中的自动装配的形式byType、byName、no、construtor、autodetect和default(beans标签中没有,而bean标签中有该属性)共6种装配的方式的举例说明
spring中的自动装配实例byName、byType,两种装配方式的实例。有需要学习的朋友可以看一下。
spring-boot自动装配原理.md
深度剖析Spring Boot自动装配机制实现原理(csdn)————程序
Spring自动装配的方式和举例、以及@Qualifier、@Autowire、@Resource的使用。
Spring 自动装配及其注解 博客:https://blog.csdn.net/u010476739/article/details/76735241
NULL 博文链接:https://cdxs2.iteye.com/blog/1941095
NULL 博文链接:https://garrincha.iteye.com/blog/2109688
Spring实现集合(List_Map_Set)_自动装配,适合初学者熟悉集合类型的装配。
主要为大家详细介绍了Java注解机制之Spring自动装配实现原理,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
众所周知Spring里面的bean就类似是定义的一个组件,而这个组件的作用就是实现某个功能的,下面这篇文章主要给大家介绍了关于spring入门教程之bean继承与自动装配的相关资料,需要的朋友可以参考借鉴,下面随着小编来...
NULL 博文链接:https://1151461406.iteye.com/blog/2389898