CDI interceptor does not work when annotation has parameter

一曲冷凌霜 提交于 2019-11-30 20:58:05

After a long story [1] ... Today I revisited this particular problem and noticed it had nothing to do with CDI:

ctx.getClass().getAnnotation(Restricted.class)

Obviously, there is no class level annotation in my example. So getAnnotation() returns null. Instead I should have used the following:

ctx.getMethod().getAnnotation(Restricted.class)

Though I don't know why there where no exceptions whatsoever. Maybe some other things were going on, that I can no longer reproduce because ...

[1] ... I migrated my application to TomEE, which was a pain in the ass and took me more than a week. And I still need to deploy TomEE in production, behind an Apache proxy, so some configuration has still to be done. If it finally works out, it might have been worth the effort, for I also had some other CDI issues (e.g. injecting CDI beans into JAX-RS services did not work using Weld + Resteasy + Tomcat) that are now gone. I also like the EJB and container managed transaction stuff. But I don't think it's a good idea to praise TomEE whenever someone tries to use small parts or even a subset of Java EE together with Tomcat. Many of its API implementations are meant to work in various environments (even Java SE) and that is a good thing. So if you don't need TomEE for some of its advanced Java EE features, I can not recommend this move. Some problems will be gone, but others will emerge. And you can no longer choose freely among different API implementations. However, this is not a statement against TomEE. Being used to Tomcat I really love the idea of TomEE.

if you switch to TomEE you'll don't need to depend (maven) on implementations, just api (use org.apache.openejb:javaee-api:6.0-4 with a provided scope

It sounds like you have things setup correct (beans.xml and interceptor). Which CDI implementation are you using? If you're using Tomcat have you looked at using TomEE?

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