Spring – Bean执行方法两次

huangapple go评论166阅读模式
英文:

Spring - Bean executes method twice

问题

以下是要翻译的内容:

SchedulingTasks

  1. @Component
  2. @EnableScheduling
  3. public class SchedulingTasks {
  4. private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
  5. @Autowired
  6. public TestClass testclass;
  7. @Scheduled(fixedDelay = 1 * 60 * 60 * 1000)
  8. public void refreshDatasets() {
  9. testclass.simpleTest();
  10. System.out.println("The time is now " + dateFormat.format(new Date()));
  11. }
  12. }

TestClass

  1. @Component
  2. public class TestClass {
  3. public void simpleTest() {
  4. System.out.println("FINISHED");
  5. }
  6. }

Configuration

  1. @Configuration
  2. @ComponentScan(basePackages = "com.some.analytics.scripts",
  3. excludeFilters = {@ComponentScan.Filter (
  4. type= FilterType.ANNOTATION,
  5. value=Configuration.class)})
  6. public class ScriptsConfig {
  7. @Bean
  8. public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
  9. PropertySourcesPlaceholderConfigurer p = new PropertySourcesPlaceholderConfigurer();
  10. p.setIgnoreResourceNotFound(true);
  11. return p;
  12. }
  13. }

Main

  1. public class Scripts {
  2. public static void main(String[] args) {
  3. AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ScriptsConfig.class);
  4. SchedulingTasks schedulingTasks = ctx.getBean(SchedulingTasks.class);
  5. schedulingTasks.refreshDatasets();
  6. }
  7. }

当运行主方法时,我得到以下输出:

  1. FINISHED The time is now 11:08:39
  2. FINISHED The time is now 11:08:39
英文:

The below method refreshDatasets is executed twice when I run the app. Any idea what I messed up with the configuration of Spring (annotation based)?

SchedulingTasks

  1. @Component
  2. @EnableScheduling
  3. public class SchedulingTasks {
  4. private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
  5. @Autowired
  6. public TestClass testclass;
  7. @Scheduled(fixedDelay = 1 * 60 * 60 * 1000)
  8. public void refreshDatasets() {
  9. testclass.simpleTest();
  10. System.out.println("The time is now " + dateFormat.format(new Date()));
  11. }
  12. }

TestClass

  1. @Component
  2. public class TestClass {
  3. public void simpleTest() {
  4. System.out.println("FINISHED");
  5. }
  6. }

Configuration

  1. @Configuration
  2. @ComponentScan(basePackages = "com.some.analytics.scripts",
  3. excludeFilters = {@ComponentScan.Filter (
  4. type= FilterType.ANNOTATION,
  5. value=Configuration.class)})
  6. public class ScriptsConfig {
  7. @Bean
  8. public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
  9. PropertySourcesPlaceholderConfigurer p = new PropertySourcesPlaceholderConfigurer();
  10. p.setIgnoreResourceNotFound(true);
  11. return p;
  12. }
  13. }

Main

  1. public class Scripts {
  2. public static void main(String[] args) {
  3. AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ScriptsConfig.class);
  4. SchedulingTasks schedulingTasks = ctx.getBean(SchedulingTasks.class);
  5. schedulingTasks.refreshDatasets();
  6. }
  7. }

When running the main method, I get the following output:

  1. FINISHED The time is now 11:08:39
  2. FINISHED The time is now 11:08:39

答案1

得分: 4

在使用@EnableScheduling注解时,Spring会在后台创建一个TaskExecutor。这将调度所有的@Scheduled方法。对于使用fixedDelay的方法,它们将立即触发执行(除非设置了initialDelay)。

您还以编程方式执行任务,因此有两个执行:

  • 由Spring执行的一个
  • 在主方法中执行的一个

您应该删除手动调用,一切都应按预期工作。

您可以在以下链接中找到更多信息:https://spring.io/guides/gs/scheduling-tasks/ 和 https://docs.spring.io/spring/docs/current/spring-framework-reference/integration.html#scheduling-task-scheduler

英文:

When using the @EnableScheduling annotation, Spring creates a TaskExecutor in the background. This will schedule all the @Scheduled methods. In the case of methods with fixedDelay, they will be fired instantly (unless initialDelay is set).

You are also programatically executing the task, so you have two executions:

  • The one executed by Spring
  • The one executed in the main method.

You should remove the manual invocation, and everything should work as expected.

You can find more information in https://spring.io/guides/gs/scheduling-tasks/ and https://docs.spring.io/spring/docs/current/spring-framework-reference/integration.html#scheduling-task-scheduler

huangapple
  • 本文由 发表于 2020年7月21日 19:09:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63013207.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定