Java technology Spring automatic assembly

Automatic assembly

1 The concept of automatic assembly

  1. Manual assembly: Specifying attribute values ​​explicitly in the form of value or ref is manual assembly.
  2. Automatic assembly: the assembly according to the specified rules, need not explicitly specified , Spring automatically attribute values match injected into the bean.

2 Assembly mode

  1. Automatic assembly based on type : inject the bean with the matching type as an attribute into another bean. If there are multiple beans of the same type as the target bean in the IOC container, Spring will not be able to determine which bean is most suitable for the attribute, so automatic assembly cannot be performed
  2. Automatic assembly based on name : The name of the target bean and the attribute name must be set exactly the same
  3. Automatic assembly through the constructor: When there are multiple constructors in the bean, this automatic assembly method will be very complicated. Not recommended.

3 Selection suggestions

Compared with the automatic assembly that uses annotations, the automatic assembly in XML documents is a little clumsy, and annotations are used more in projects.

Java training

Guess you like

Origin blog.csdn.net/msjhw_com/article/details/109283536