Property Value injection or constructor injection?

Source: Internet
Author: User

The following factors should be taken into account for the two IOC injection methods (attribute value injection and constructor injection:

1. Using property value injection makes it easy to process default values and any values. In this case, you do not have to set each property value. constructor injection can easily generate multiple constructor and call each other. multiple constructors or parameters make the program lengthy and difficult to manage.

2. Using property value injection (the property value is not private) will automatically inherit the subclass, but gouzaohanshu cannot. the latter restriction allows the subclass to create only one template function related to the parent class, and it will call the parent class constructor. however, no matter which method is used, most ides now generate constructors or attributes easily.

3. Using property value injection to generate Java documents is easier than constructor injection.

4. In the runtime environment, attribute value injection can use reflection to operate attributes using attribute names. however, for Constructor injection, the compiled class file does not maintain the parameter space of the constructor. Therefore, it is impossible to automatically call the object based on name matching.

5. Property Value injection allows you to obtain and set attribute states (values). This situation is beneficial to many situations, such as the persistence of attribute states to any storage media.

6. Property Value injection can use the propertyeditor mechanism to automatically convert data types as needed. Spring supports this situation.

7. Property Value injection is easy to change because the setter method can be called multiple times.

......................... The translation is too bad. forget it !.......

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.