@Asynchronous 是否在抽象 class 中工作?

Does @Asynchronous work in abstract class?

我有一个带有抽象方法的抽象class

@Asynchronous
public abstract void runAsync();

我在 Spring

中找到了 @Async 的答案

问题是,如果我在实现中覆盖它,这个方法runAsync会是异步的吗?还是我只需要在实现中做 @Asynchronous 注释?

注释默认不继承。只有在注释定义中具有 @Inherited 属性的注释才会被继承。现在查看 @Async 注释定义:

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

Async注解没有@Inherited属性,所以不会继承到子类。在这种情况下,您需要在子类覆盖方法中显式指定 @Async 以使其工作。欲了解更多信息,请访问 link.

编辑:javax.ejb.Asynchronous 也没有 @Inherited 属性 (docs)

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

因此,在 @Asynchronous 的情况下,使用 @Asynchronous 覆盖方法的行为将与上述相同。