简体   繁体   中英

Java and @annotations

There's something I'm not sure to understand when it comes to using Java annotations. Here is an example :

I create a @Log annotation and add some functionality with it (every method annotated with @Log runs some log before executing the method).

Now I'm creating a new @SuperLog annotation like this one :

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Log
public @interface SuperLog {
     ............
}

This @SuperLog must provide all the stuff @Log does, plus some extra stuff specific to @SuperLog.

Unfortunately when I'm executing some methods annotated with @SuperLog, the log specific to @Log doesn't execute.

I don't understand why : the fact @SuperLog is annotated with @Log doesn't mean it "inherits" properties from @Log ? Shouldn't @SuperLog do every thing @Log is supposed to do?

As this question outlines, there is no inheritance of annotations.

And beyond that: keep in mind that annotations (mainly) get meaning at runtime because some framework reacts to their presence.

In other words: you could create a framework that somehow supports annotations coming with an inheritance tree. But assuming you are working with some existing framework, you have to accept what this framework is doing.

I guess you 'execute some methods annotated @SuperLog' means use 'Spring AOP'.

As GhostCat said, the inheritance is depends on framework's implementation. And unfortunately Spring AOP pointcut doesn't support meta-annotation yet.

You can follow this spring improvement .

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