Order of constructor, @Autowired, @PostConstruct in spring

Source: Internet
Author: User

Dependency Injection:

Literally, you know, to inject object B into object A, you first have to generate object B and object A to perform the injection.

So, if there is a member variable B in a Class A that is @autowired annotated, then @autowired injection occurs after the construction method of a has finished executing.

If you want to do some initialization when you build the object,

It happens that these classes are initialized and dependent on other class injections, and cannot be implemented in constructors.

To do this, you can use the @postconstruct annotation method to complete the initialization, and the method of @PostConstruct annotations will be called automatically after the dependency injection is complete.

Constructor >> @Autowired >> @PostConstruct

The popular point is that in Class A when initializing, you want to do something else in the process, but because Class A needs a Class B injection and cannot do it in the constructor, use @PostConstruct to do so.

Order of constructor, @Autowired, @PostConstruct in spring

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.