Does @Asynchronous work in abstract class?

Jacek Wójtowicz :

I've got an abstract class with abstract method

@Asynchronous
public abstract void runAsync();

I found this answer for @Async in Spring Async not working on controller's abstract super class method

Question is, will this method runAsync be asynchronous If I Override it in implementation? Or do I need to make @Asynchronous annotation only in implementation?

Amit Bera :

Annotations are not inherited by default. Annotations will be inherited only if it has @Inherited properties in the annotation definition. Now look at the @Async annotation definition :

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Async 

Async annotation does not have the properties @Inherited, so it will not be inherited to subclass. In that case, you need to explicitly specify the @Async in the subclass override method to make it work. For more info please visit the link.

EDIT: javax.ejb.Asynchronous also does not has @Inherited property (docs)

@Target(value={METHOD,TYPE})
 @Retention(value=RUNTIME)
public @interface Asynchronous

So, in the case of @Asynchronous, the behavior in case of method override with @Asynchronous will be same as mentioned above.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=305703&siteId=1