Spring Boot 2.0.0.M6中的弹簧调度
我已经使用Spring初始值设定项、嵌入式Tomcat、Thymeleaf模板引擎和作为可执行JAR文件的包生成了一个Spring引导web应用程序 使用的技术: 弹簧靴2.0.0.M6,Java 8,maven 我创建了这个类Spring Boot 2.0.0.M6中的弹簧调度,spring,spring-mvc,spring-boot,scheduled-tasks,scheduling,Spring,Spring Mvc,Spring Boot,Scheduled Tasks,Scheduling,我已经使用Spring初始值设定项、嵌入式Tomcat、Thymeleaf模板引擎和作为可执行JAR文件的包生成了一个Spring引导web应用程序 使用的技术: 弹簧靴2.0.0.M6,Java 8,maven 我创建了这个类 com.iberia.task.scheduled public class IberiaAssetHandlerJob { private static final SimpleDateFormat dateFormat = new Si
com.iberia.task.scheduled
public class IberiaAssetHandlerJob {
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
@Scheduled(cron = "0 * * * * ?")
public void reportCurrentTime() {
System.out.println("The time is now {}" + dateFormat.format(new Date()));
}
}
希望每分钟都能看到消息
我的主课呢
@SpringBootApplication
@EnableScheduling
@Import({SecurityConfig.class})
public class IberiaWebUtilsApplication {
public static void main(String[] args) {
SpringApplication.run(IberiaWebUtilsApplication.class, args);
}
}
但是我在控制台中没有看到任何消息您应该使用记录器更改
System.out.println(…)
,以便查看消息并在其回答中声明类为Spring托管bean
private static final Logger log = Logger.getLogger(IberiaAssetHandlerJob.class);
log.info("The time is now {}" + dateFormat.format(new Date()));
您应该使用记录器更改
System.out.println(…)
,以便查看消息并在其答案中将类声明为Spring托管bean
private static final Logger log = Logger.getLogger(IberiaAssetHandlerJob.class);
log.info("The time is now {}" + dateFormat.format(new Date()));
@Scheduled
注释仅适用于Spring管理的bean。您没有将@Component
注释放在IberiaAssetHandlerJob
之上,也没有手动创建bean(使用@bean
注释)
另外,请注意
PrintStream
(System.out使用的底层类),因此如果您还有其他线程写入System.out
,那么您可能会得到奇怪的结果,这就是为什么建议使用日志框架的原因(如他的回答中所述)。这还允许您使用占位符,如{}
,例如:
logger.info("The time is now {}", dateFormat.format(new Date()));
在这种情况下,请确保使用多个参数,而不是将所有参数连接到单个字符串。注释
@Scheduled
仅适用于Spring管理的bean。您没有将@Component
注释放在IberiaAssetHandlerJob
之上,也没有手动创建bean(使用@bean
注释)
另外,请注意
PrintStream
(System.out使用的底层类),因此如果您还有其他线程写入System.out
,那么您可能会得到奇怪的结果,这就是为什么建议使用日志框架的原因(如他的回答中所述)。这还允许您使用占位符,如{}
,例如:
logger.info("The time is now {}", dateFormat.format(new Date()));
在这种情况下,请确保使用多个参数,而不是将所有参数连接到单个字符串