views:

22

answers:

1

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"&gt;
   <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?

+1  A: 

I don't think you can. You can however spare a bit of typing by using stereotypes:

@Named
@RequestScoped
@Secure
@Stereotype
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Secure {

}

And then annotate your beans with only @Secure

Bozho