Spring Auto-Wiring Beans
来源:互联网 发布:spss编程计算概率 编辑:程序博客网 时间:2024/05/22 03:51
http://www.mkyong.com/spring/spring-auto-wiring-beans-in-xml/
Spring Auto-Wiring Beans
In Spring framework, you can wire beans automatically with auto-wiring feature. To enable it, just define the “autowire” attribute in <bean>.
<bean id="customer" class="com.mkyong.common.Customer" autowire="byName" />
In Spring, 5 Auto-wiring modes are supported.
- no – Default, no auto wiring, set it manually via “ref” attribute
- byName – Auto wiring by property name. If the name of a bean is same as the name of other bean property, auto wire it.
- byType – Auto wiring by property data type. If data type of a bean is compatible with the data type of other bean property, auto wire it.
- constructor – byType mode in constructor argument.
- autodetect – If a default constructor is found, use “autowired by constructor”; Otherwise, use “autowire by type”.
Examples
A Customer and Person object for auto wiring demonstration.
package com.mkyong.common; public class Customer {private Person person; public Customer(Person person) {this.person = person;} public void setPerson(Person person) {this.person = person;}//...}
package com.mkyong.common; public class Person {//...}
1. Auto-Wiring ‘no’
This is the default mode, you need to wire your bean via ‘ref’ attribute.
<bean id="customer" class="com.mkyong.common.Customer"> <property name="person" ref="person" /></bean> <bean id="person" class="com.mkyong.common.Person" />
2. Auto-Wiring ‘byName’
Auto-wire a bean by property name. In this case, since the name of “person” bean is same with the name of the “customer” bean’s property (“person”), so, Spring will auto wired it via setter method – “setPerson(Person person)
“.
<bean id="customer" class="com.mkyong.common.Customer" autowire="byName" /> <bean id="person" class="com.mkyong.common.Person" />
See full example – Spring Autowiring by Name.
3. Auto-Wiring ‘byType’
Auto-wire a bean by property data type. In this case, since the data type of “person” bean is same as the data type of the “customer” bean’s property (Person object), so, Spring will auto wired it via setter method – “setPerson(Person person)
“.
<bean id="customer" class="com.mkyong.common.Customer" autowire="byType" /> <bean id="person" class="com.mkyong.common.Person" />
See full example – Spring Autowiring by Type.
4. Auto-Wiring ‘constructor’
Auto-wire a bean by property data type in constructor argument. In this case, since the data type of “person” bean is same as the constructor argument data type in “customer” bean’s property (Person object), so, Spring auto wired it via constructor method – “public Customer(Person person)
“.
<bean id="customer" class="com.mkyong.common.Customer" autowire="constructor" /> <bean id="person" class="com.mkyong.common.Person" />
See full example – Spring Autowiring by Constructor.
5. Auto-Wiring ‘autodetect’
If a default constructor is found, uses “constructor”; Otherwise, uses “byType”. In this case, since there is a default constructor in “Customer” class, so, Spring auto wired it via constructor method – “public Customer(Person person)
“.
<bean id="customer" class="com.mkyong.common.Customer" autowire="autodetect" /> <bean id="person" class="com.mkyong.common.Person" />
See full example – Spring Autowiring by AutoDetect.
It’s always good to combine both ‘auto-wire’ and ‘dependency-check’ together, to make sure the property is always auto-wire successfully.
<bean id="customer" class="com.mkyong.common.Customer" autowire="autodetect" dependency-check="objects /> <bean id="person" class="com.mkyong.common.Person" />
Conclusion
In my view, Spring ‘auto-wiring’ make development faster with great costs – it added complexity for the entire bean configuration file, and you don’t even know which bean will auto wired in which bean.
In practice, i rather wire it manually, it is always clean and work perfectly, or better uses @Autowired annotation, which is more flexible and recommended.
http://www.mkyong.com/spring/spring-auto-wiring-beans-in-xml/
- Spring Auto-Wiring Beans
- Spring Auto-Wiring Beans
- Spring(9)-Spring Auto-Wiring Beans with @Autowired annotation
- Spring之 Auto-Wiring All Beans of Compatible Type
- (十二)Beans自动装配(Auto-Wiring)
- Spring学习笔记 使用XML配置实现Bean的auto-wiring (自动绑定)
- Spring学习笔记 使用annotation配置实现Bean的auto-wiring (自动绑定)
- Spring Boot自动配置(Auto-Configuration),@EnableAutoConfiguration,Spring Beans和依赖注入
- spring beans
- spring beans
- spring+hibernate+struts例子wiring安装
- spring beans beanfactory applicationcontext
- spring beans 标签
- spring beans.xml
- java spring Inner Beans
- spring beans源码解读
- Spring beans架构
- spring beans源码解读
- 蓝桥杯-扑克序列问题
- JavaWeb编程:获取的计算机的版本号以及浏览器的版本之类的(${header['User-Agent']})
- CI框架入门中的简单MVC例子
- Hadoop ha简介
- org.codehaus.plexus.archiver.jar.Manifest.merge(org.codehaus.plexus.archiver.jar.Manifest)错误原因
- Spring Auto-Wiring Beans
- HDU 2112 HDU Today (map+dijkstra)
- spring配置文件说明
- 硬币游戏 一枚、两枚、还是四枚
- 第2周项目-课后实践·阅读程序(2)
- 算法导论计数排序python实现
- iOS实现图文混排效果
- 【KM算法】【最大乘积生成树】[HNOI2014] bzoj3571 画框
- 黑马程序员 Java高兴技术----反射机制