[cdi-dev] @PostConstruct and @Inject methods in superclasses

Pete Muir pmuir at redhat.com
Sat Oct 1 23:16:59 EDT 2011


There is no need to refer to the interceptors spec to understand this. But the discussions about the interceptors spec is interesting :-)

>> But what about my example 4, the shared @Inject method + @PostConstruct case?
>> Do we need to invoke all superclass injection first? thus do all @Inject fields + @Inject methods + @PostConstruct first, then the one from the Horse class?
>> Could we also specify that all @Inject methods needs to get called before @PostConstruct?

JSR-330 says that injected fields happen before methods, and always superclass-first i.e.

class X extends Y { ... }

any injected field in Y, followed by any injected method in Y followed by any injected field in X followed by any injected method in X.

CDI then covers the @PostConstruct in 5.5.2 of the CDI spec.

The container must ensure that:
•	Initializer methods declared by a class X in the type hierarchy of the bean are called after all injected fields declared by X or by superclasses of X have been initialized, and after all Java EE component environment resource dependencies declared by X or by superclasses of X have been injected.
•	Any @PostConstruct callback declared by a class X in the type hierarchy of the bean is called after all initializer meth- ods declared by X or by superclasses of X have been called, after all injected fields declared by X or by superclasses of X have been initialized, and after all Java EE component environment resource dependencies declared by X or by su- perclasses of X have been injected.

Thus, 

You can be assured that the @PostConstruct for Y happens after injection, and the same for X. However you can't be certain that the @PostConstruct for Y happens before the injection for X.

Do we need to clarify this? I'm not sure we do? If we do, why?

On 1 Oct 2011, at 06:21, Stuart Douglas wrote:

> 
> On 01/10/2011, at 7:47 PM, Mark Struberg wrote:
> 
>> Oki found it there:
>>> most general superclass first.
>> of course the interceptors_spec_1_1 only defines this for 'Lifycycle interceptors' 
> 
> The spec is not very clear on this point, but @PostConstruct methods on a bean are considered lifecycle interceptors.

Correct. I think this would need clarifying in the interceptors spec - Mark could you file an EJB spec issue for this?

> There is actually a fair bit of deviation between CDI and the interceptors and Managed bean spec on some points: 
> 
> - CDI beans don't support interceptor methods on the beans themselves (at least Weld does not, the interceptors spec allows you to have an @AroundInvoke method on the managed bean itself).

Wow, I totally missed that before. I think this is just a bug in Weld. Could you create an issue?

> - @Resource @Psersitence* @EJB etc annotation of CDI managed beans do not create JNDI bindings like they are supposed to according to the EE platform spec.

This is partly a Weld bug, partly a Java EE integration issue, which never got resolved properly. The CDI spec doesn't define this, where is it in Java EE? If it's there, then it's an impl bug, not a spec clarification I think.

> 
> As far as I know no one actually really uses these 'features' anyway, but it is a deviation from the spec that we should probably clarify.
> 
> Stuart
> 




More information about the cdi-dev mailing list