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.

转载于:https://www.cnblogs.com/ghgyj/p/4749904.html

Spring Auto-Wiring Beans相关推荐

  1. Spring 注入内部 Beans

    转载自   Spring 注入内部 Beans 注入内部 Beans 正如你所知道的 Java 内部类是在其他类的范围内被定义的,同理,inner beans 是在其他 bean 的范围内定义的 be ...

  2. spring wiring beans

    In Spring, objects aren't responsible for finding or creating the other objects that they need to do ...

  3. spring入门:beans.xml不提示、别名、创建对象的三种方式

    spring的版本是2.5 一.beans.xml文件不提示 Location:spring-framework-2.5.6.SEC01\dist\resources\spring-beans-2.5 ...

  4. 【Spring心得】xmlns=“http://www.spring 和 xmlns:beans=“http://www.spring 差之毫厘谬以千里

    运行java测试代码时出现报错,控制台卡住了: 五月 03, 2018 9:11:10 下午 org.springframework.context.support.ClassPathXmlAppli ...

  5. Spring自动装配Beans

    在Spring框架,可以用 auto-wiring 功能会自动装配Bean.要启用它,只需要在 <bean>定义"autowire"属性. <bean id=&q ...

  6. 学好spring系列之Wiring Bean(装配Bean)

    在上两篇的博文中,分别对spring框架中的两个很重要的概念DI(Dependency Injection)和AOP(Aspect-Oriented programming)做了介绍,或许不那么完美, ...

  7. java+spring+mysql配置_用spring的beans配置mysql数据库

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www. ...

  8. Spring(6)---自动装配Beans

    Spring自动装配Beans 在Spring框架,可以用 auto-wiring 功能会自动装配Bean.要启用它,只需要在 <bean>定义"autowire"属性 ...

  9. 这些Spring面试题,你都会了吗?

    转载自   这些Spring面试题,你都会了吗? 问题一: 什么是Spring? Spring是Enterprise Java的开源开发框架.Spring Framework的核心功能可用于开发任何J ...

最新文章

  1. 上三角矩阵的特征值分解
  2. matlab 数据保存为txt excel mat
  3. OSG+VS2010+win7环境搭建---OsgEarth编译
  4. 【虾说区块链】入门区块链,先学分布式系统!一文说明分布式系统与区块链的关系...
  5. QT - Could not load the Qt platform plugin xcb in even though it was found.
  6. bool类型_Python基本数据类型之Number
  7. Go各种类型转换及函数的高级用法
  8. Linux环境下如何使用 ffmpeg 处理音视频的合成问题
  9. mysql五日均线代码_通达信均线源码,5日通达信均线公式源码
  10. b站黑马程序员的python怎么样_如何评价传智播客旗下的黑马程序员?
  11. 短视频剪辑如何入门?给初学者的三个小建议,助你快速入门
  12. STM32控制舵机讲解,从入门到放弃。
  13. 入行 AI,如何选个脚踏实地的岗位?
  14. 网站部署证书 百度浏览器仍提示不安全网站怎么办
  15. 【SpringMVC】编程式事务控制,基于 XML 的声明式事务控制,基于注解的声明式事务控制
  16. php序列化 与json_PHP中serializen()与json_encode()的性能差异
  17. 如何在线批量将PDF转换成JPG格式
  18. Chinese-ELECTRA ‘adam_m not found in checkpoint ‘
  19. JVM系列之深入理解JVM(三)
  20. ArcGIS Pro发布WebScene

热门文章

  1. PBI中使用SVG自定义折线缩略图
  2. 系统集成项目管理工程师学习经验集【60,59】
  3. ScalingLayout
  4. 再介绍一家美国华人创立的超级独角兽
  5. 一个想踏入游戏行业的人的独白
  6. 江铖:乳腺癌识别By AI
  7. 2021年安全员-C证考试内容及安全员-C证实操考试视频
  8. 博图v17在win11系统下仿真跳出failed to launch simulation望有人能解决一下
  9. 使用Fiddle对夜神模拟器进行抓包
  10. Spring Cloud/Boot启动报错Consider defining a bean of type ServerCodecConfigurer