我有一个CDI原型,其中包含一些InterceptorBinding如下:-
@Inherited
@InterceptorBinding
@Retention(RetentionPolicy.RUNTIME)
@Target({
ElementType.METHOD,
ElementType.TYPE})
@Documented
public @interface MyInterceptable {
}
@Interceptor
@MyInterceptable
public class MyInterceptor {
@AroundInvoke
public Object perform(InvocationContext context) throws Exception {
log.info("My Interceptor begin.");
Object result =context.proceed();
log.info("My Interceptor end.");
return result;
}
}
@Stereotype
@MyInterceptable
@Retention(RetentionPolicy.RUNTIME)
@Target({
ElementType.TYPE
})
@Documented
public @interface MyStereoable {
}
当我在非ejb上定义这个原型时,它是正确工作的。消息在doing1()执行前后打印。
@Singleton
@MyStereoable
public class MyCustomized {
public void doning1(){
//print something.
}
}
无论如何,当我尝试将此与无状态EJB一起使用时,它不起作用。拦截器没有打印任何消息。
@Remote
public interface HelloServiceable extends Serializable {
void doning2();
}
@Stateless
@MyStereoable
public class HelloService implements HelloServiceable {
public void doing2() {
//Print something
}
}
然后将case 1和case2混合如下:-
@Stateless
@MyStereoable
public class HelloService implements HelloServiceable {
@Inject
private MyCustomized myBean;
public void doing2() {
this.myBean.doing1();
//Print something
}
}
MyCustomized可以被拦截并打印消息,但对于无状态EJB则不行。
我不确定我是否对CDI和EJB有误解或困惑。你能进一步提供建议吗?事先非常感谢您的帮助。我期待尽快收到你的来信。
问候,
Charlee Ch .
我已经将我的项目从EAR/EJB项目更改为独立的Web项目。CDI构造型现在与EJB会话Bean一起工作。
我在github上创建了一个小演示。请注意,它使用JBoss Arquillian进行测试。
我希望这些信息可能有用。
问候,
Charlee Ch .