在我JEE6-CDI-web应用程序里面,我宣布一个安全拦截器,像这样的:配置拦截器被用于所有CDI-豆类应用
//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豆相应:
//CDI bean using Inteceptor
@Named @RequestScoped
@Secure
public class TestBean {
public String doStuff() {
}
}
现在我问自己,我一定要标注我所有的CDI-Beans都使用这个拦截器?或者是否有一种方法可以将beans.xml配置为对所有CDI bean使用拦截器,而无需为每个单独的bean声明它?
这只适用于EE7,但不失良好的提示 – 2015-08-07 15:31:04