Spring Automatic Assembly
This is the code we wrote earlier, we used the P namespace in the code.
and using manual assembly, the car
<bean id="Address" class="cn.bdqn.spring.Address" p:ciyt= "Beijing" p:street="Malianwa"></bean> <bean id="car" class="Cn.bdqn.spring.Car" p:brand= "Ford" p:price="10000 " ></bean> <bean id="person" class="Cn.bdqn.spring.Person" p:name= "Zhangsna " p:age=" p:address-ref=" "address" p:car-ref="Car"> </bean> |
Automatic bean assembly in XML configuration
The Spring IOC container can automatically assemble beans. What needs to be done is only in <bean>
How to assemble automatically:
Automatic assembly mode specified in the Autowire attribute
①bytype (Auto-assemble by type): If there are multiple beans in the IOC container that are consistent with the target bean type, spring will not be able to determine which Bean is best for the property, so it cannot perform automatic assembly.
②byname (Auto-assemble by name): The name and property name of the target bean must be set exactly the same.
③constructor (auto-assemble via constructor, deprecated): when there are multiple constructors in Bena. This automatic assembly method will be complex
Disadvantages of automatic bean assembly in XML configuration
① Setting the Autowire property for automatic assembly in the bean configuration file will assemble all of the bean's properties, however, the Aotuwire property is not flexible if you want to assemble individual properties.
The ②autowire property is either automatically assembled by type or automatically assembled by name, not both.
③ in general, the automatic assembly function is rarely used in actual projects, as it is more convincing to have a clear and clear configuration document than the benefits of the automatic assembly function.
Automatic bean assembly in Spring XML configuration