简体   繁体   中英

Java EE: Bind some interceptor annotations to a single one

First of all: I want to use Java EE not Spring! I have some self defined annotations which are acting as interceptor bindings. I use the annotations on my methods like this:

@Logged
@Secured
@RequestParsed
@ResultHandled
public void doSomething() {
   // ...
}

For some methods I want to use a single of these annotations but most methods I want to use like this:

@FunctionMethod
public void doSomething() {
   // ...
}

Can I bundle these set of annotations to a single one? I cannot write the code in a single interceptor because for some methods I want to use them seperately too.

I know that there is a @Stereotype definition possible, but as far as I know, this is used to define a whole class not a single method.

I would say, that you are on the right pass with a stereotype.

It's right, that the examples one finds and also the official Java EE 6 Tutorial only uses it on a class as an example (eg @Model), but you may as well declare @TYPE(MEHOD) in your custom annotation and then I assume that it works.

With help of some well-known search engine I found the solution in the documentation of JBoss Weld (Chapter 9.6 Interceptor binding with inheritance)

I can use an interceptor binding interface which is inherited from other interceptor bindings. It will look like this:

@InterceptorBinding
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Logged
@Secured
@RequestParsed
@ResultHandled
public @interface FunctionMethod {
  // clean and empty
}

Now I can use the new interceptor binding on the bean method and all of the interceptors will be called:

@FunctionMethod
public void doSomething() {
   // ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM