All @Resource injection before any @PostConstruct again
Asked Answered
C

1

7

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.

Cuprous answered 14/7, 2010 at 9:16 Comment(0)
S
9

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.

Spiritualize answered 14/7, 2010 at 10:14 Comment(1)
Thanks Kenny :) That explains it.Cuprous

© 2022 - 2024 — McMap. All rights reserved.