配置拦截器以用于应用程序内的所有 CDI-Bean
在我的 JEE6-CDI-webapp 中,我声明了一个安全拦截器,如下所示:
//Secure.java
@Inherited
@Target({TYPE, METHOD})
@Retention(RUNTIME)
@InterceptorBinding
public @interface Secure
{}
//SecurityInterceptor.java
@Secure
@Interceptor
public class SecurityInterceptor
{
@AroundInvoke
protected Object invoke(InvocationContext ctx) throws Exception
{
// do stuff
ctx.proceed();
}
}
并在 beans.xml 中声明它:
//beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
<alternatives/>
<decorators/>
<interceptors>
<class>com.profitbricks.security.SecurityInterceptor</class>
</interceptors>
</beans>
要使用它,我相应地注释 CDI-bean:
//CDI bean using Inteceptor
@Named @RequestScoped
@Secure
public class TestBean {
public String doStuff() {
}
}
现在我问自己,我是否必须注释所有我的 CDI-Beans 使用这个拦截器?或者有没有办法配置 beans.xml 以对我的所有 CDI-beans 使用拦截器,而不必为每个单独的 bean 声明它?
In my JEE6-CDI-webapp, i declared a Security Interceptor, like this one:
//Secure.java
@Inherited
@Target({TYPE, METHOD})
@Retention(RUNTIME)
@InterceptorBinding
public @interface Secure
{}
//SecurityInterceptor.java
@Secure
@Interceptor
public class SecurityInterceptor
{
@AroundInvoke
protected Object invoke(InvocationContext ctx) throws Exception
{
// do stuff
ctx.proceed();
}
}
And declared it inside the beans.xml:
//beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
<alternatives/>
<decorators/>
<interceptors>
<class>com.profitbricks.security.SecurityInterceptor</class>
</interceptors>
</beans>
To use it, i annotate a CDI-bean accordingly:
//CDI bean using Inteceptor
@Named @RequestScoped
@Secure
public class TestBean {
public String doStuff() {
}
}
Now i ask myself, do i have to annotate ALL my CDI-Beans to use this interceptor? Or is there a way to configure the beans.xml to use the interceptor for all my CDI-beans, without having to declare it for every single bean?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为你不能。但是,您可以通过使用构造型来节省一些打字时间:
然后仅使用
@Secure
注释您的 beanI don't think you can. You can however spare a bit of typing by using stereotypes:
And then annotate your beans with only
@Secure
您可以尝试使用我几个月前编写的小型 CDI 扩展:
https://github.com/struberg/InterDyn
这将允许您通过正则表达式样式动态地将 CDI 拦截器应用到一堆类。
它很快就会成为 Apache MyFaces CODI 的一部分,我只需要先找一些时间清理配置部分;)
You could try to use the small CDI Extension I wrote a few months ago:
https://github.com/struberg/InterDyn
This will allow you to dynamically apply CDI Interceptors to a bunch of classes via regexp style.
It will soon be part of Apache MyFaces CODI, I just need to find some time to cleanup the config part first ;)
这些可能会晚一些,但我遇到了一个需要全局/应用程序范围拦截器的要求。
要启用应用程序拦截器,请将拦截器注释为:
在这种情况下,好消息是您不必在 beans.xml 中声明拦截器:
Oracle javaee7 教程
These may be late, but i encountered a requirement for which i needed a global/application wide interceptor.
To enable application interceptor, annotate the interceptor as:
In this case, the good news is that you dont have to declare the interceptor in the beans.xml:
Oracle javaee7 tutorial
所有的豆子都没有用。您可以在引导期间操作 bean - 例如 codi-addons 的 ultra_lite_ejbs(请参阅 bitbucket org)使用它。也许这对你来说是一个灵感。恕我直言,例如像 openwebbeans.apache.org 这样的社区将更适合您的 CDI 相关问题。
All beans aren't useful. You can manipulate beans during bootstrapping - e.g. ultra_lite_ejbs at codi-addons (see bitbucket org) uses it. Maybe it's an inspiration for you. IMHO e.g. a community like openwebbeans.apache.org would be a better fit for your CDI related questions.