gpt4 book ai didi

Java Spring 计划作业不起作用

转载 作者:行者123 更新时间:2023-12-05 00:17:53 24 4
gpt4 key购买 nike

我有一个应该运行预定代码的网络应用程序:

package com.myproject.daemon.jobs;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class MyDaemonJob {

private static final Logger log = LoggerFactory.getLogger(MyDaemonJob.class);

@PostConstruct
public void init() {
log.info("MyDaemonJob is intialized " );
}

@Scheduled(fixedDelay = 1000)
public void startDaemon() {
try {
log.info("MyDaemonJob is running ...");
} catch (Exception e) {
log.error("Encountered error running scheduled job: " + e.getMessage());
}
}
}

正如我从 PostConstruct 中看到的那样,它肯定被识别为 Spring bean 并被初始化。日志。然而该方法与 @Scheduled注释从不运行,尽管它应该每 1 秒运行一次。

这是应用程序上下文 xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">

<context:component-scan base-package="
com.myproject.daemon.jobs,
com.myproject.product" />

</beans>

最佳答案

谢谢大家的快速帮助。这真的很有帮助。

代码开始工作,一旦我添加了带有注释的配置类,如下所示——

package com.myproject;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

@Configuration
@EnableScheduling
public class AppConfig {
// various @Bean definitions
}

关于Java Spring 计划作业不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39198577/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com