[英]JEE: How to intercept a @PostCostruct method?
I have a bean having:我有一个豆子:
void initialize()
method annotated with @PostConstruct
.@PostConstruct
注释的void initialize()
方法。void release()
method annotated with @PreDestroy
.@PreDestroy
注释的void release()
方法。@Interceptors
annotation defining some interceptors.@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.@AroundConstruct
方法进入。 InvocationContext: Target is null
, method is null
, constructor is set. null
,方法为null
,构造函数已设置。@AroundConstruct
method.@AroundConstruct
方法存在。 InvocationContext: Target is the bean instance, method is null
, constructor is set. null
,构造函数被设置。@PostConstruct
method is called, calls proceed() and returns.@PostConstruct
方法被调用,调用proceed() 并返回。 InvocationContext: Target is the bean instance, method is null
, constructor is set. null
,构造函数被设置。@PostConstruct
method is called.@PostConstruct
方法被调用。 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.我很惊讶地发现
@PostConstruct
不是在 bean 的@PostConstruct
方法调用期间调用,而是在 bean 的构造和调用 bean 的@PostConstruct
方法之间调用。 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.此外,bean 的
@PostConstruct
方法的调用根本没有被拦截,拦截器的@PostConstruct
方法和它的@AroundInvoke
方法都没有拦截。
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)?有没有办法拦截 bean 的
@PostConstruct
方法( @PreDestroy
方法也是如此)?
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.
此外,稍后调用堆栈深处的其他方法也很高兴知道该调用是由容器通过这两种方法之一触发的。
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):由于我在互联网上找不到任何答案,我进行了一些调试并发现了以下内容(使用 WildFly 15.0.1.Final):
When a bean is instantiated:当 bean 被实例化时:
@AroundConstruct
(InvocationContext: Constructor set)@AroundConstruct
(InvocationContext: Constructor set)@AroundConstruct
(InvocationContext: Constructur and target set)@AroundConstruct
(InvocationContext: Constructur and target set)@PostConstruct
(InvocationContext: Constructur and target set)@PostConstruct
(InvocationContext: Constructur and target set)@PostConstruct
@PostConstruct
@PostConstruct
(InvocationContext: Constructur and target set)@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.这意味着你不知道调用的是哪个方法,你只知道调用了bean的
@PostConstruct
方法。 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.我想这是因为 bean 的
@PostConstruct
方法是作为某种拦截器执行的,但这只是我的假设。
When you execute a bean's method:当你执行一个 bean 的方法时:
@AroundInvoke
(InvocationContext: Method and target set)@AroundInvoke
(InvocationContext:方法和目标集)@AroundInvoke
(InvocationContext: Method and target set)@AroundInvoke
(InvocationContext:方法和目标集) When a bean is destroyed:当 bean 被销毁时:
@PreDestroy
(InvocationContext: Target set)@PreDestroy
(InvocationContext: Target set)@PreDestroy
@PreDestroy
@PreDestroy
(InvocationContext: Target set)@PreDestroy
(InvocationContext: Target set) I hope that this will be also helpful to others.我希望这对其他人也有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.