简体   繁体   English

JEE:如何拦截@PostCostruct 方法?

[英]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()方法。
  • Some other methods.其他一些方法。
  • In addition that bean has a @Interceptors annotation defining some interceptors.此外,该 bean 有一个@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:调用顺序如下所示:

  • Interceptor's @AroundConstruct method is entered.拦截器的@AroundConstruct方法进入。 InvocationContext: Target is null , method is null , constructor is set. InvocationContext:目标为null ,方法为null ,构造函数已设置。
  • Beans constructor is called. Beans 构造函数被调用。
  • The call exists through Interceptor's @AroundConstruct method.该调用通过 Interceptor 的@AroundConstruct方法存在。 InvocationContext: Target is the bean instance, method is null , constructor is set. InvocationContext:目标是 bean 实例,方法是null ,构造函数被设置。
  • Interceptor's @PostConstruct method is called, calls proceed() and returns.拦截器的@PostConstruct方法被调用,调用proceed() 并返回。 InvocationContext: Target is the bean instance, method is null , constructor is set. InvocationContext:目标是 bean 实例,方法是null ,构造函数被设置。
  • After the previous call returned completely the bean's @PostConstruct method is called.在前一个调用完全返回后,bean 的@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 被实例化时:

  • Entering interceptor's @AroundConstruct (InvocationContext: Constructor set)进入拦截器的@AroundConstruct (InvocationContext: Constructor set)
  • Executing bean's constructor执行 bean 的构造函数
  • Leaving interceptor's @AroundConstruct (InvocationContext: Constructur and target set)离开拦截器的@AroundConstruct (InvocationContext: Constructur and target set)
  • Entering interceptor's @PostConstruct (InvocationContext: Constructur and target set)进入拦截器的@PostConstruct (InvocationContext: Constructur and target set)
  • Executing bean's @PostConstruct执行 bean 的@PostConstruct
  • Leaving interceptor's @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 的方法时:

  • Entering interceptor's @AroundInvoke (InvocationContext: Method and target set)进入拦截器的@AroundInvoke (InvocationContext:方法和目标集)
  • Executing bean's method执行bean的方法
  • Leaving interceptor's @AroundInvoke (InvocationContext: Method and target set)离开拦截器的@AroundInvoke (InvocationContext:方法和目标集)

When a bean is destroyed:当 bean 被销毁时:

  • Entering interceptor's @PreDestroy (InvocationContext: Target set)进入拦截器的@PreDestroy (InvocationContext: Target set)
  • Executing bean's @PreDestroy执行 bean 的@PreDestroy
  • Leaving interceptor's @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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM