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" />

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" />

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" />

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" />

Note

. 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.

最新文章

  1. Entity Framework 6 Recipes 2nd Edition(10-5)译 -&gt; 在存储模型中使用自定义函数
  2. UWP开发之控件:用WebView做聊天框
  3. centos svn 升级
  4. CentOS下MySQL数据库安装
  5. [ACM_水题] UVA 11292 Dragon of Loowater [勇士斗恶龙 双数组排序 贪心]
  6. hdu 4612 Warm up 桥缩点
  7. Xamarin 后台持续定位与提示
  8. linux安装QQ
  9. 不同版本(2.3,2.4,2.5) web.xml 的web-app头信息
  10. HDFS Architecture
  11. Taurus.MVC 2.2.3.4 :WebAPI 实现权限控制认证(及功能增强说明)
  12. XYZZY spfa 最长路 判环
  13. python爬虫学习笔记(一)——环境配置(windows系统)
  14. C# System.Threading.ReaderWriterLockSlim
  15. mongodb用mongoose查库的对象,不能增加属性
  16. nc工具学习
  17. jquery 事件小事例
  18. JavaScript中模块化工具require.js
  19. 【转】Android单帧动画Rotate旋转
  20. Achartengine.jar绘制动态图形-饼图

热门文章

  1. /usr/lib/libstdc++.so.6: version `GLIBCXX_3.4.15&#39; not found错误的解决
  2. Android开发之关于ListView中adapter调用notifyDataSetChanged无效的原因
  3. hdu 4911 Inversion (分治 归并排序 求逆序数)
  4. Codeforces Round #247 (Div. 2) C. k-Tree (dp)
  5. 前端JS对后台传递的timestamp的转换
  6. onkeypress与onkeydown及 oncopy和onpaste 事件区别详细说明
  7. UVa 11584 Partitioning by Palindromes【DP】
  8. 《分销系统-原创第一章》之“多用户角色权限访问模块问题”的解决思路( 位运算 + ActionFilterAttribute )
  9. OK335xS psplash Screen 移植
  10. TCP/IP详解学习笔记(14)-TCP连接的未来和性能(未写完)