Automatic bean assembly in Spring XML configuration

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.