Java CDI. Interceptor is only invoked in the first method call in a class [duplicate]

老子叫甜甜 提交于 2019-12-05 09:39:16

It's because you are calling methodB() directly and not via the CDI proxy so the interceptor is never invoked. Interceptors will only be invoked when the CDI bean method is called using its proxy. You should move method B into another CDI bean and @Inject it into this one and from methodA change methodB() to bean2.methodB(..).

Use self injection. Bean self injection can be achieved in CDI quite easily - just inject an Instance, where T is the implementation.

@Named
public class Foo implements Fooable{

@Inject

private Instance<Foo> foo;

public void executeFirst(){

foo.get().executeSecond();

}

@Transactional

public void executeSecond(){

//do something

}

}

This way, you can execute your method right in the same bean. Make sure to select right scope if the bean is stateful. Also make sure Instance's generic type T is directly the implementation - this ensures implementation of the correct object every time.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!