Spring@postconstruct and @predestroy Explanatory notes

Source: Internet
Author: User

# @PostConstruct Annotations Using

# # @PostConstructApi Instructions for use
The > Postconstruct comment is used to perform any initialization on a method that needs to be executed after dependency injection is complete. This method must be called before the class is put into service. All classes that support dependency injection must support this comment. Even if the class does not have a request to inject any resources, the method that is annotated with postconstruct must also be called. There is only one way to comment with this comment. The method of applying the postconstruct annotation must comply with all of the following criteria: The method should not have any parameters, except in the case of the EJB Interceptor (Interceptor), in which case it will have a invocationcontext, as defined by the EJB specification. The return type of the method must be void, the method should not throw a checked exception, the method applied postconstruct can be public, protected, the package private, or private, except for the application client. The method cannot be static, the method can be final, and if the method throws an unchecked exception, the class must not be placed in the service unless it is an EJB that can handle the exception and recover from it.

In summary, it is important to note that the use of @postconstruct is:
> * Method No parameters
> * Method no return value must be void
> * Method cannot be static
> * Method cannot throw an exception
> * method executes only once
> * Using @postconstruct method can only have one in the Springbean.

# # Springbean Initialization process
"Flow
St=>start: Start
op1=>operation:spring Container Loading 1
Op2=>operation: Call constructor
Op3=>operation: @PostConstruct method call
Op4=>operation:init () call
Op5=>operation: Other code
Op6=>operation:destroy () call
Op7=>operation: @PreDestroy method call
E=>end: End

St->op1->op2->op3->op4->op5->op6->op7->e

```

@PostConstruct method is executed after the method call is constructed before the init () method.

# # Project Application Scenario
Typically used for some system configuration or for loading of cached data.

# @PreDestroy Annotations
...

[email protected] and @predestroy comments

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.