十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專業(yè)推廣+無(wú)憂售后,網(wǎng)站問題一站解決
本篇文章給大家分享的是有關(guān)利用spring boot怎么對(duì)第三方j(luò)ar包進(jìn)行加載,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

成都創(chuàng)新互聯(lián)公司主要從事成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)古田,十年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):13518219792
具體方法如下:
接下來的任務(wù),就是如何將上面的xml配置文件,去配置化。
從上面的配置文件中,可以得出,我們需要配置3個(gè)實(shí)例,分別是JobDetail,JobTrigger和Scheduler。
1、首先抽取出需要在application.properties配置文件中配置的屬性項(xiàng),從上面的配置文件中,可以得出如下需要配置的屬性項(xiàng),對(duì)應(yīng)的VO如下:
package com.chhliu.springboot.quartz.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix="quartz.config")
public class QuartzConfigProperties {
private String targetObject;
private String targetMethod;
private boolean concurrent;
private String cronExpression;
private String applicationContextSchedulerContextKey;
private boolean waitForJobsToCompleteOnShutdown;
……省略getter、setter方法……
}
2、在application.properties配置文件中,加入如下配置
quartz.config.targetObject=taskJob ## 待執(zhí)行對(duì)象的名字 quartz.config.targetMethod=doJob ## 待執(zhí)行的方法的名字 quartz.config.concurrent=false ## 是否并發(fā),如果上一個(gè)定時(shí)任務(wù)還沒有執(zhí)行完,又被觸發(fā)了,如果配置為false,則需等待上個(gè)任務(wù)執(zhí)行完,才觸發(fā) quartz.config.cronExpression=0/5 * * * * ? ## 任務(wù)觸發(fā)表達(dá)式 quartz.config.applicationContextSchedulerContextKey=applicationContextKey ## 通過該key可以獲取spring上下文 quartz.config.waitForJobsToCompleteOnShutdown=true ## 是否等待任務(wù)完全執(zhí)行完后,再銷毀線程池
3、分別實(shí)例化JobDetail,JobTrigger和Scheduler
package com.chhliu.springboot.quartz.entity;
import org.quartz.Trigger;
import org.springframework.beans.factory.annotation.Autowired;
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;
import com.chhliu.springboot.quartz.config.QuartzConfigProperties;
/**
* 描述:將quartz的xml配置文件去配置化
* @author chhliu
* 創(chuàng)建時(shí)間:2017年4月11日 下午7:41:21
* @version 1.2.0
*/
@Configuration
public class QuartzConfig {
@Autowired
private QuartzConfigProperties properties; // 注入屬性配置文件對(duì)應(yīng)的類實(shí)例
/**
* attention:
* Details:初始化JobDetail
* @author chhliu
* 創(chuàng)建時(shí)間:2017年4月11日 下午6:17:06
* @param task
* @return
* MethodInvokingJobDetailFactoryBean
* @throws ClassNotFoundException
* @throws IllegalAccessException
* @throws InstantiationException
*/
@Bean(name = "jobDetail")
public MethodInvokingJobDetailFactoryBean detailFactoryBean() throws ClassNotFoundException, InstantiationException, IllegalAccessException {// ScheduleTask為需要執(zhí)行的任務(wù)
MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();
/*
* 是否并發(fā)執(zhí)行
* 例如每5s執(zhí)行一次任務(wù),但是當(dāng)前任務(wù)還沒有執(zhí)行完,就已經(jīng)過了5s了,
* 如果此處為true,則下一個(gè)任務(wù)會(huì)執(zhí)行,如果此處為false,則下一個(gè)任務(wù)會(huì)等待上一個(gè)任務(wù)執(zhí)行完后,再開始執(zhí)行
*/
jobDetail.setConcurrent(properties.isConcurrent());
/*
* 為需要執(zhí)行的實(shí)體類對(duì)應(yīng)的對(duì)象
*/
String targetObject = properties.getTargetObject();
jobDetail.setTargetBeanName(targetObject);
/*
* 通過這幾個(gè)配置,告訴JobDetailFactoryBean我們需要定時(shí)執(zhí)行targetObject類中的properties.getTargetMethod()方法
*/
jobDetail.setTargetMethod(properties.getTargetMethod());
return jobDetail;
}
/**
* attention:
* Details:實(shí)例化JobTrigger
* @author chhliu
* 創(chuàng)建時(shí)間:2017年4月11日 下午7:39:14
* @param jobDetail
* @return
* CronTriggerFactoryBean
*/
@Bean(name = "jobTrigger")
public CronTriggerFactoryBean cronJobTrigger(MethodInvokingJobDetailFactoryBean jobDetail) {
CronTriggerFactoryBean tigger = new CronTriggerFactoryBean();
tigger.setJobDetail(jobDetail.getObject());
tigger.setCronExpression(properties.getCronExpression());
return tigger;
}
/**
* attention:
* Details:實(shí)例化Scheduler
* @author chhliu
* 創(chuàng)建時(shí)間:2017年4月11日 下午7:39:35
* @param cronJobTrigger
* @return
* SchedulerFactoryBean
*/
@Bean(name = "scheduler")
public SchedulerFactoryBean schedulerFactory(Trigger cronJobTrigger) {
SchedulerFactoryBean bean = new SchedulerFactoryBean();
// 注冊(cè)觸發(fā)器
bean.setTriggers(cronJobTrigger);
// 通過applicationContextSchedulerContextKey屬性配置獲取spring上下文
bean.setApplicationContextSchedulerContextKey(properties.getApplicationContextSchedulerContextKey());
// 關(guān)閉任務(wù)的時(shí)候,是否等待任務(wù)執(zhí)行完畢
bean.setWaitForJobsToCompleteOnShutdown(properties.isWaitForJobsToCompleteOnShutdown());
return bean;
}
} 4、編寫需要執(zhí)行的方法
package com.chhliu.springboot.quartz.job;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
@Service("taskJob")
public class TaskJob {
private static final Logger LOGGER = LoggerFactory.getLogger(TaskJob.class);
public void doJob(){
LOGGER.info("hello spring boot, i'm the king of the world!!!");
}
}
5、測(cè)試
package com.chhliu.springboot.quartz;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import com.chhliu.springboot.quartz.config.QuartzConfigProperties;
@SpringBootApplication
@EnableConfigurationProperties({QuartzConfigProperties.class} ) // 開啟配置屬性支持
public class SpringbootQuartzApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootQuartzApplication.class, args);
}
}
6、測(cè)試結(jié)果如下
2017-04-11 19:09:35.017 INFO 7500 --- [eduler_Worker-1] c.chhliu.springboot.quartz.job.TaskJob : hello spring boot, i'm the king of the world!!! 2017-04-11 19:09:40.004 INFO 7500 --- [eduler_Worker-2] c.chhliu.springboot.quartz.job.TaskJob : hello spring boot, i'm the king of the world!!! 2017-04-11 19:09:45.004 INFO 7500 --- [eduler_Worker-3] c.chhliu.springboot.quartz.job.TaskJob : hello spring boot, i'm the king of the world!!! 2017-04-11 19:09:50.004 INFO 7500 --- [eduler_Worker-4] c.chhliu.springboot.quartz.job.TaskJob : hello spring boot, i'm the king of the world!!! 2017-04-11 19:09:55.001 INFO 7500 --- [eduler_Worker-5] c.chhliu.springboot.quartz.job.TaskJob : hello spring boot, i'm the king of the world!!! 2017-04-11 19:10:00.002 INFO 7500 --- [eduler_Worker-6] c.chhliu.springboot.quartz.job.TaskJob : hello spring boot, i'm the king of the world!!! 2017-04-11 19:10:05.001 INFO 7500 --- [eduler_Worker-7] c.chhliu.springboot.quartz.job.TaskJob : hello spring boot, i'm the king of the world!!!
以上就是利用spring boot怎么對(duì)第三方j(luò)ar包進(jìn)行加載,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。