计划任务是否应该在每次执行时启动Spring应用程序?

计划任务是否应该在每次执行时启动Spring应用程序?,spring,spring-boot,Spring,Spring Boot,我正在运行一个带有计划任务的spring启动应用程序。它分为两个maven项目,我们称它们为“lib”和“app” 其方法由@Scheduled注释的类位于“lib”项目中。它不包含任何用@SpringBootApplication注释的类,也不包含任何启动spring应用程序的类。以下是Scheduledbean的外观: @Component public class Refresher { @Scheduled(fixedRate = 30000) public void

我正在运行一个带有计划任务的spring启动应用程序。它分为两个maven项目,我们称它们为“lib”和“app”

其方法由
@Scheduled
注释的类位于“lib”项目中。它不包含任何用
@SpringBootApplication
注释的类,也不包含任何启动spring应用程序的类。以下是
Scheduled
bean的外观:

@Component
public class Refresher {

    @Scheduled(fixedRate = 30000)
    public void refresh() {
        ...
    }
“lib”项目还有一个简单的
SchedulingConfigurer
实现,它告诉Spring将
ThreadPoolTaskScheduler
与其计划的任务一起使用

另一方面,“app”项目依赖于“lib”,有一个用
@SpringBootApplication
注释的类,并像往常一样启动它:

@SpringBootApplication(scanBasePackages = {"x.y", "x.y.z"})
@EnableScheduling
public class ExampleService {

    public static void main(String[] args) {
        SpringApplication.run(ExampleService.class, args);
    }
}
两个maven项目都有以下特点:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.5.RELEASE</version>
    <relativePath/>
</parent>
这很好,它告诉我主应用程序已经启动。但是,每次执行Refresher时,我每30秒都会收到以下几行代码:

2019-01-11 18:18:58.596  INFO 6428 --- [nio-8080-exec-3] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@66e31414: startup date [Fri Jan 11 18:18:58 MSK 2019]; root of context hierarchy
2019-01-11 18:18:58.637  INFO 6428 --- [nio-8080-exec-3] trationDelegate$BeanPostProcessorChecker : Bean 'configurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$7c355e31] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
... some logging from the refresh() method
2019-01-11 18:19:01.020  INFO 6428 --- [nio-8080-exec-3] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1262db58: startup date [Fri Jan 11 18:19:01 MSK 2019]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@66e31414
2019-01-11 18:19:01.035  INFO 6428 --- [nio-8080-exec-3] o.s.boot.SpringApplication               : Started application in 3.585 seconds (JVM running for 73.771)
2019-01-11 18:19:01.035  INFO 6428 --- [nio-8080-exec-3] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@1262db58: startup date [Fri Jan 11 18:19:01 MSK 2019]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@66e31414
2019-01-11 18:19:01.035  INFO 6428 --- [nio-8080-exec-3] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@66e31414: startup date [Fri Jan 11 18:18:58 MSK 2019]; root of context hierarchy

注意“在3.585秒内启动了应用程序(JVM运行73.771)”。这种情况每30秒发生一次。看起来,为了运行
Scheduled
bean,Spring每次都运行一个新的单独的Spring应用程序。它为什么会这样做,这能避免吗?
Scheduled
bean可以作为当前启动的Spring Boot应用程序的一部分运行吗?

您的
刷新程序具体做什么。它本质上是调用本地执行器以检查服务器上的配置更改(使用Spring cloud config server)。然后它分析配置,就这样。调用该端点将重新启动上下文。。。因此,事实上你看到了你所看到的\@戴纳姆先生,是的,谢谢。我还发现了这一点,这也证实了这一点。所以,从服务器重新获取配置的唯一方法是定期刷新执行器,这会导致定期重新启动应用程序。听起来不太对劲。。。您同意吗?假设您使用的是
springcloudconfig
服务器,您也可以调用它来获取最新的配置。
/refresh
的整个要点是刷新
@refeshoped
bean,这意味着应用程序的重新配置部分。唯一的方法是(部分)重新启动应用程序。
2019-01-11 18:18:58.596  INFO 6428 --- [nio-8080-exec-3] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@66e31414: startup date [Fri Jan 11 18:18:58 MSK 2019]; root of context hierarchy
2019-01-11 18:18:58.637  INFO 6428 --- [nio-8080-exec-3] trationDelegate$BeanPostProcessorChecker : Bean 'configurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$7c355e31] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
... some logging from the refresh() method
2019-01-11 18:19:01.020  INFO 6428 --- [nio-8080-exec-3] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1262db58: startup date [Fri Jan 11 18:19:01 MSK 2019]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@66e31414
2019-01-11 18:19:01.035  INFO 6428 --- [nio-8080-exec-3] o.s.boot.SpringApplication               : Started application in 3.585 seconds (JVM running for 73.771)
2019-01-11 18:19:01.035  INFO 6428 --- [nio-8080-exec-3] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@1262db58: startup date [Fri Jan 11 18:19:01 MSK 2019]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@66e31414
2019-01-11 18:19:01.035  INFO 6428 --- [nio-8080-exec-3] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@66e31414: startup date [Fri Jan 11 18:18:58 MSK 2019]; root of context hierarchy