This article is a mirror article of machine translation, please click here to jump to the original article.

View: 16470|Reply: 0

[Source] Java annotations@PostConstruct and detailed explanations and examples of @PreDestroy

[Copy link]
Posted on 4/4/2019 4:21:12 PM | | | |
Brief introduction

Java EE5 introduces two annotations, @PostConstruct and @PreDestroy, that act on the servlet lifecycle, enabling custom operations before the bean is initialized and before it is destroyed. This article mainly explains @PostConstruct.

API Usage Instructions

The following are the instructions for using the @PostConstruct API:

PostConstruct annotations are used on methods that need to be executed after dependency injection is complete to perform any initialization. This method must be called before putting the class into the service. All classes that support dependency injection must support this annotation. Even if the class doesn't request any resource injection, the method annotated with PostConstruct must be called. There is only one way to annotate with this annotation. The method for applying PostConstruct annotations must adhere to all of the following criteria: the method must not have any parameters unless in the case of an EJB interceptor, as defined by the EJB specification, in which case it will come with an InvocationContext object; The return type of the method must be void; The method must not throw a checked exception; The method of applying PostConstruct can be public, protected, package private, or private; The method cannot be static except for the application client; The method can be final; If the method throws an unchecked exception, then the class must not be placed in the service unless it is an EJB that can handle the exception and recover from it.
It can be summarized as follows:

  • There is only one way to annotate with this annotation;
  • The annotated method must not have any parameters;
  • The annotated method returns a value of void;
  • The annotated method must not throw a checked exception;
  • The annotated method must be a non-static method;
  • This method is only performed once;


Servlet execution flow chart

The addition of two annotations will only allow the servlet to execute the flowchart:



During the instantiation process of a specific bean, the annotated method @PostConstruct is called after the construct method and before the init method.

Example

Executable methods written based on Spring boot can be found on github:https://github.com/HappySecondBrother/example

UserService method (provides cached data):

BusinessService method, calling UserService by @PostConstruct:

Execution results:

Project application

In the project@PostConstruct the main application scenario is to load some cache data when initializing the servlet.

Notes:

Using this annotation affects the start time of the service. The server scans all files in WEB-INF/classes and all jar packages under WEB-INF/lib at startup.





Previous:Unable to inject @Autowired in Listener
Next:High-performance mysql 3rd edition Chinese PDF
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com