繁体   English   中英

Spring Boot如何识别子类方法上的@Scheduled?

[英]How does Spring Boot recognize @Scheduled on a subclass' method?

让我们以一个简单的Spring Boot程序为例:

应用程序

@SpringBootApplication
@EnableScheduling
public class Application {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class);
    }
}

超类.java

public abstract class SuperClass {

    @Scheduled(fixedRate = 5000)
    public void printSomething() {
        System.out.println("this is the super method");
    }
}

子类

@Component
public class SubClass extends SuperClass {

}

根据答案,子类将仅继承由@Inherited注释的注释,而@Scheduled没有此类注释。 那么这怎么起作用呢?

@Inherited仅适用于类类型,不适用于方法。

请注意,如果带注释的类型用于注释除类之外的任何内容,则此元注释类型无效。 还要注意,此元注释仅使注释从超类继承; 已实现的接口上的注释无效。

当Spring扫描bean中的@Scheduled注释(或其他注释)时,它将在bean类中查找所有方法。 SubClass具有printSomething因此Spring决定可以通过调度行为来增强它。


Spring处理@Scheduled的方式与标准代理机制略有不同,并且能够调用带有注释的private方法。

如果您重写了子类中的printSomething方法,并在该声明上省略了@Scheduled注释,那么Spring将不会应用调度行为。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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