Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

All @Resource injection before any @PostConstruct again

JSR-250 says all @Resource annotated methods will be called before the @PostConstruct method..

My question is:

Does that mean that all @Resource annotated methods on all beans in a context will be called before any @PostConstruct annotated methods are called? Or in other words can a beans @PostConstruct method be called once its dependencies have been injected even if other beans in the context still haven't had there dependences injected?

Regards, Tim.

like image 742
Tim P Avatar asked Jul 14 '10 09:07

Tim P


People also ask

What is the @PostConstruct annotation used for?

Annotation Type PostConstruct The PostConstruct annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization. This method MUST be invoked before the class is put into service. This annotation MUST be supported on all classes that support dependency injection.

What is the use of @PostConstruct in spring boot?

When we annotate a method in Spring Bean with @PostConstruct annotation, it gets executed after the spring bean is initialized. We can have only one method annotated with @PostConstruct annotation. This annotation is part of Common Annotations API and it's part of JDK module javax.

What is the difference between PostConstruct and dependency injection?

The PostConstruct annotation is used on a method that needs to be executed after. dependency injection is done to perform any initialization. This method MUST be. invoked before the class is put into service. This annotation MUST be supported on all. classes that support dependency injection.

What happens when @PostConstruct annotation is destroyed in spring?

Likewise, when the context is destroyed, we may have to close some resources utilized by spring beans. So generally, whenever we annotate a method in Spring Bean with @PostConstruct annotation, it gets executed after the spring bean is initialized. We can have only one method annotated with @PostConstruct annotation.

How to solve the @PostConstruct method annotation problem in Java?

This problem can be solved either by JSR-330 Dependency Injection for Java constructor injection or JSR 250 Common Annotations for the Java @PostConstruct method annotation. JSR-250 defines a common set of annotations which has been included in Java SE 6. dependency injection is done to perform any initialization. This method MUST be

What is the use of @PostConstruct?

@PostConstruct is an annotation used on a method that needs to be executed after dependency injection is done to perform any initialization. The following application demonstrates the usage of @PostConstruct.


1 Answers

It is guaranteed that when a given bean's @PostConstruct gets called, that all of its @Resource fields will have been injected. If any of those injections are themselves beans with their own @Resource and @PostConstruct, then those will have already been called. In other words, by the time any given @PostConstruct is called, it is guaranteed that all of its dependencies have been fully initialized.

It is possible, and in fact likely, that BeanA will be constructed and initialized via @PostConstruct before BeanB has even been instantiated, if BeanB has no expressed dependency on BeanA.

like image 107
skaffman Avatar answered Sep 27 '22 03:09

skaffman