Spring 简明教程
Spring - Beans Auto-Wiring
你已了解如何使用 <bean> 元素声明 bean,并使用 XML 配置文件中的 <constructor-arg> 和 <property> 元素注入 <bean>。
You have learnt how to declare beans using the <bean> element and inject <bean> using <constructor-arg> and <property> elements in XML configuration file.
Spring 容器可以在不使用 <constructor-arg> 和 <property> 元素的情况下确定协作 bean 之间的关系,这有助于减少为大型基于 Spring 的应用程序编写的 XML 配置量。
The Spring container can autowire relationships between collaborating beans without using <constructor-arg> and <property> elements, which helps cut down on the amount of XML configuration you write for a big Spring-based application.
Autowiring Modes
以下是自动装配模式,可以用于指示 Spring 容器对依赖项注入使用自动装配。你可以使用 <bean/> 元素的 autowire 属性来为 bean 定义指定 autowire 模式。
Following are the autowiring modes, which can be used to instruct the Spring container to use autowiring for dependency injection. You use the autowire attribute of the <bean/> element to specify autowire mode for a bean definition.
Sr.No |
Mode & Description |
1 |
*no*This is default setting which means no autowiring and you should use explicit bean reference for wiring. You have nothing to do special for this wiring. This is what you already have seen in Dependency Injection chapter. |
2 |
byNameAutowiring by property name. Spring container looks at the properties of the beans on which autowire attribute is set to byName in the XML configuration file. It then tries to match and wire its properties with the beans defined by the same names in the configuration file. |
3 |
byTypeAutowiring by property datatype. Spring container looks at the properties of the beans on which autowire attribute is set to byType in the XML configuration file. It then tries to match and wire a property if its type matches with exactly one of the beans name in configuration file. If more than one such beans exists, a fatal exception is thrown. |
4 |
constructorSimilar to byType, but type applies to constructor arguments. If there is not exactly one bean of the constructor argument type in the container, a fatal error is raised. |
5 |
*autodetect*Spring first tries to wire using autowire by constructor, if it does not work, Spring tries to autowire by byType. |
你可以使用 byType 或 constructor 自动装配模式来连接数组和其他类型集合。
You can use byType or constructor autowiring mode to wire arrays and other typed-collections.
Limitations with autowiring
当在整个项目中一致使用时,自动装配效果最好。如果通常不使用自动装配,开发人员仅将其用于连接一个或两个 Bean 定义可能会令人迷惑。尽管如此,自动装配可以显著减少指定属性或构造函数参数的需要,但你应在使用自动装配前考虑其局限性和缺点。
Autowiring works best when it is used consistently across a project. If autowiring is not used in general, it might be confusing for developers to use it to wire only one or two bean definitions. Though, autowiring can significantly reduce the need to specify properties or constructor arguments but you should consider the limitations and disadvantages of autowiring before using them.
Sr.No. |
Limitations & Description |
1 |
Overriding possibility You can still specify dependencies using <constructor-arg> and <property> settings which will always override autowiring. |
2 |
Primitive data types You cannot autowire so-called simple properties such as primitives, Strings, and Classes. |
3 |
Confusing nature Autowiring is less exact than explicit wiring, so if possible prefer using explict wiring. |