Reputation: 65
I have a bean having:
void initialize()
method annotated with @PostConstruct
.void release()
method annotated with @PreDestroy
.@Interceptors
annotation defining some interceptors.One of those interceptors has methods annotated with
@AroundConstruct
@AroundInvoke
@AroundTimeout
@PostConstruct
@PreDestroy
In each of those methods I put some logging, so I can see which and when the interceptor methods are called. And the call order looks like this:
@AroundConstruct
method is entered.
InvocationContext: Target is null
, method is null
, constructor is set.@AroundConstruct
method.
InvocationContext: Target is the bean instance, method is null
, constructor is set.@PostConstruct
method is called, calls proceed() and returns.
InvocationContext: Target is the bean instance, method is null
, constructor is set.@PostConstruct
method is called.I was very surprised realizing that the @PostConstruct
is not called during the bean's @PostConstruct
method call, but between the construction of the bean and calling the bean's @PostConstruct
method. Also the call of the bean's @PostConstruct
method is not intercepted at all, not by the interceptor's @PostConstruct
method nor bi its @AroundInvoke
method.
Is there any mistake on my side / my programs side?
Is there any way to intercept the bean's @PostConstruct
method (same goes for the @PreDestroy
method)?
I need to prepare the context(s) and fill them with some content. In addition it would be also nice for other method deep down the call stack later to know that the call was triggered by the container through one of those two methods.
Upvotes: 1
Views: 667
Reputation: 65
As I couldn't find any answer on that in the Internet, I went through some debugging and found out the following (used WildFly 15.0.1.Final):
When a bean is instantiated:
@AroundConstruct
(InvocationContext: Constructor set)@AroundConstruct
(InvocationContext: Constructur and target set)@PostConstruct
(InvocationContext: Constructur and target set)@PostConstruct
@PostConstruct
(InvocationContext: Constructur and target set)This means that you don't know which method is called, you only know that the @PostConstruct
method of the bean is called. I guess that's because the @PostConstruct
method of the bean is executed as some kind of interceptor, but that is only an assumption on my side.
When you execute a bean's method:
@AroundInvoke
(InvocationContext: Method and target set)@AroundInvoke
(InvocationContext: Method and target set)When a bean is destroyed:
@PreDestroy
(InvocationContext: Target set)@PreDestroy
@PreDestroy
(InvocationContext: Target set)I hope that this will be also helpful to others.
Upvotes: 1