java中的接口是类吗
308
2023-01-19
详解Quartz 与 Spring框架集成的三种方式
XML+ Spring MVC 版本
POM文件
web.xml
classpath*:/spring/spring-core.xml
spring.xml
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:p="http://springframework.org/schema/p" xmlns:tx="http://springframework.org/schema/tx" xmlns:context="http://springframework.org/schema/context" xmlns:aop="http://springframework.org/schema/aop" xmlns:task="http://springframework.org/schema/task" xsi:schemaLocation="http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-4.1.xsd http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-4.1.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-4.1.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.1.xsd http://springframework.org/schema/task http://springframework.org/schema/task/spring-task-4.0.xsd " >
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:p="http://springframework.org/schema/p"
xmlns:tx="http://springframework.org/schema/tx"
xmlns:context="http://springframework.org/schema/context"
xmlns:aop="http://springframework.org/schema/aop"
xmlns:task="http://springframework.org/schema/task"
xsi:schemaLocation="http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-4.1.xsd
http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-4.1.xsd
http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-4.1.xsd
http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.1.xsd
http://springframework.org/schema/task
http://springframework.org/schema/task/spring-task-4.0.xsd "
>
spring-mvc.xml
xmlns="http://springframework.org/schema/beans" xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:p="http://springframework.org/schema/p" xmlns:mvc="http://springframework.org/schema/mvc" xmlns:context="http://springframework.org/schema/context" xmlns:aop="http://springframework.org/schema/aop" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.1.xsd http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-3.1.xsd http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-4.1.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.1.xsd">
xmlns="http://springframework.org/schema/beans"
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:p="http://springframework.org/schema/p"
xmlns:mvc="http://springframework.org/schema/mvc"
xmlns:context="http://springframework.org/schema/context"
xmlns:aop="http://springframework.org/schema/aop"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans-3.1.xsd
http://springframework.org/schema/mvc
http://springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://springframework.org/schema/aop
http://springframework.org/schema/aop/spring-aop-4.1.xsd
http://springframework.org/schema/context
http://springframework.org/schema/context/spring-context-3.1.xsd">
spring-quartz.xml
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd">
HelloTask
package com.adagio;
import org.springframework.stereotype.Component;
@Component
public class HelloTask {
public void excute() {
System.out.println("excute...22222>>>>>>>>");
}
}
configuration + Spring MVC
和上面类似,只是 spring-quartz.xml 转成configuration
package com.adagio;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
@Configuration
public class QuartzConfig {
@Bean
public SchedulerFactoryBean factoryBean() {
SchedulerFactoryBean factoryBean = new SchedulerFactoryBean();
factoryBean.setTriggers(cronTriggerFactoryBean().getObject());
return factoryBean;
}
@Bean
public CronTriggerFactoryBean cronTriggerFactoryBean() {
CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
cronTriggerFactoryBean.setJobDetail(methodInvokingJobDetailFactoryBean().getObject());
cronTriggerFactoryBean.setCronExpression("0/5 * * * * ?");
return cronTriggerFactoryBean;
}
@Bean
public MethodInvokingJobDetailFactoryBean methodInvokingJobDetailFactoryBean() {
MethodInvokingJobDetailFactoryBean m = new MethodInvokingJobDetailFactoryBean();
m.setTargetObject(helloTask());
m.setTargetMethod("excute");
return m;
}
@Bean
public HelloTask helloTask() {
return new HelloTask();
}
}
Configuration + Spring Boot
QuartzConfig 和 HelloTask 与上面一样
启动方式不同
package com.adagio;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan(basePackages= {"com.adagio"})
@SpringBootApplication
public class TimerTaskApplication {
public static void main(String[] args) {
SpringApplication.run(TimerTaskApplication.class, args);
}
}
总结
对xml的方式比较熟悉,所有先配置好xml
confugration如果不是Spring Boot的项目的话,还是用的比较少
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~