Spring Beans automatic assembly

1.1 Spring automatic assembly 'ByName'

  In the configuration file of our configuration file Bean, Bean if the property is set up autowire = "ByName" means the automatic equipment. Spring means that the container looks attributes (attributes corresponding Pojo class) and set the configuration Bean to Bean value corresponding to this property. Popular terms is that the entity class attributes want to get in there and do not want to re-write properties in the Bean configuration. Figure you will understand

 Figure I (Setter method using internal injection)

Figure II (using automatic assembly autowire byName)

 

1.2Spring automatic assembling 'ByType'

This mode is specified by the attribute type automatic assembly. Spring container seen as beans, beans in the XML configuration file  autowire  property to  byType . Then, if it's  type  exactly matches the name of the configuration file beans (id) one, it will try to match and connect its properties. If a match is found, it will inject these beans, otherwise it will throw an exception. Figure

Figure I (in Pojo attribute type)

Figure II (autowire automatic assembly using the byType)

2.1Spring automatic assembly constructor. This mode  byType  very similar, but it applies to constructor arguments. (Not much description)

Mode Name

description

No

The default setting, which means there is no automatic assembly

byName

Assembled automatically by attribute name. Spring container seen in XML configuration file bean property for automatic assembly of byName . Then try to match, and its attributes are defined as the same name in the configuration file beans an attribute of connection.

byType

Automatically assembled by the attribute data type. Spring container seen in XML configuration file bean property of the automatic assembly of the byType . Then if it's the type of an exact match the profile of bean name, it will try to match the type and connection properties. If there is more than one such bean , then a fatal exception will be thrown.

Constructor

Similar to the byType , but applicable to the type constructor parameter types. If you do not have a constructor argument type in a container bean , it is a fatal error will occur.

autodetect

Spring first attempt constructor is connected using automatic assembly, if it does not perform, Spring attempt byType automatically assembled.

总结:Spring 容器可以在不使用<property> 元素的情况下自动装配相互协作的 bean 之间的关系,这有助于减少编写一个大的基于 Spring 的应用程序的 XML 配置的数量。

 

Guess you like

Origin www.cnblogs.com/ysource/p/12421142.html