spring boot加載第三方j(luò)ar包的配置文件的方法
前言
今天收到一封郵件,大概內(nèi)容如下:spring boot鼓勵去配置化,那么怎么將第三方j(luò)ar包中的xml去配置化了?
其實,這個問題,在前面的文章中也有提到,http://www.dbjr.com.cn/article/125700.htm
下面,我們就以Quartz定時任務(wù)為例,單獨對這個問題來進行說明,如何實現(xiàn)去配置化。
如果不使用spring boot,我們配置一個簡單的定時任務(wù)時,需要引入以下配置文件:
<!-- 配置需要定時執(zhí)行的任務(wù)類以及方法 -->
<bean id="doJob"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 指定任務(wù)類 -->
<property name="targetObject" ref="schedulerTask" />
<!-- 指定任務(wù)執(zhí)行的方法 -->
<property name="targetMethod" value="doTask" />
<property name="concurrent" value="false"></property>
</bean>
<!-- 配置觸發(fā)器 -->
<bean id="jobTrigger"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="doJob" />
<!-- 每5秒運行一次 -->
<property name="cronExpression" value="0/5 * * * * ?" />
</bean>
<!-- 觸發(fā)定時任務(wù) -->
<bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="jobTrigger" /><!-- 此處可以配置多個觸發(fā)器 -->
</list>
</property>
<property name="applicationContextSchedulerContextKey" value="applicationContextKey" />
<property name="waitForJobsToCompleteOnShutdown" value="true"></property>
</bean>
接下來的任務(wù),就是如何將上面的xml配置文件,去配置化。
從上面的配置文件中,可以得出,我們需要配置3個實例,分別是JobDetail,JobTrigger和Scheduler。
1、首先抽取出需要在application.properties配置文件中配置的屬性項,從上面的配置文件中,可以得出如下需要配置的屬性項,對應(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í)行對象的名字 quartz.config.targetMethod=doJob ## 待執(zhí)行的方法的名字 quartz.config.concurrent=false ## 是否并發(fā),如果上一個定時任務(wù)還沒有執(zhí)行完,又被觸發(fā)了,如果配置為false,則需等待上個任務(wù)執(zhí)行完,才觸發(fā) quartz.config.cronExpression=0/5 * * * * ? ## 任務(wù)觸發(fā)表達式 quartz.config.applicationContextSchedulerContextKey=applicationContextKey ## 通過該key可以獲取spring上下文 quartz.config.waitForJobsToCompleteOnShutdown=true ## 是否等待任務(wù)完全執(zhí)行完后,再銷毀線程池
3、分別實例化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)建時間:2017年4月11日 下午7:41:21
* @version 1.2.0
*/
@Configuration
public class QuartzConfig {
@Autowired
private QuartzConfigProperties properties; // 注入屬性配置文件對應(yīng)的類實例
/**
* attention:
* Details:初始化JobDetail
* @author chhliu
* 創(chuàng)建時間: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ù),但是當前任務(wù)還沒有執(zhí)行完,就已經(jīng)過了5s了,
* 如果此處為true,則下一個任務(wù)會執(zhí)行,如果此處為false,則下一個任務(wù)會等待上一個任務(wù)執(zhí)行完后,再開始執(zhí)行
*/
jobDetail.setConcurrent(properties.isConcurrent());
/*
* 為需要執(zhí)行的實體類對應(yīng)的對象
*/
String targetObject = properties.getTargetObject();
jobDetail.setTargetBeanName(targetObject);
/*
* 通過這幾個配置,告訴JobDetailFactoryBean我們需要定時執(zhí)行targetObject類中的properties.getTargetMethod()方法
*/
jobDetail.setTargetMethod(properties.getTargetMethod());
return jobDetail;
}
/**
* attention:
* Details:實例化JobTrigger
* @author chhliu
* 創(chuàng)建時間: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:實例化Scheduler
* @author chhliu
* 創(chuàng)建時間:2017年4月11日 下午7:39:35
* @param cronJobTrigger
* @return
* SchedulerFactoryBean
*/
@Bean(name = "scheduler")
public SchedulerFactoryBean schedulerFactory(Trigger cronJobTrigger) {
SchedulerFactoryBean bean = new SchedulerFactoryBean();
// 注冊觸發(fā)器
bean.setTriggers(cronJobTrigger);
// 通過applicationContextSchedulerContextKey屬性配置獲取spring上下文
bean.setApplicationContextSchedulerContextKey(properties.getApplicationContextSchedulerContextKey());
// 關(guān)閉任務(wù)的時候,是否等待任務(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、測試
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、測試結(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!!!
從上面的測試結(jié)果可以看出,任務(wù)被觸發(fā)了,也得到了正確的結(jié)果。
上面的這個示例,只是一個簡單的例子,但是生產(chǎn)上復(fù)雜的需求,原理也是類似的。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
設(shè)計模式之構(gòu)建(Builder)模式 建造房子實例分析
構(gòu)建模式主要用來針對復(fù)雜產(chǎn)品生產(chǎn),分離部件構(gòu)建細節(jié),以達到良好的伸縮性,考慮到設(shè)計模式來源于建筑學,因此舉一個建造房子的例子,需要的朋友可以參考下2012-12-12
springboot手動事務(wù)回滾的實現(xiàn)代碼
這篇文章主要介紹了springboot手動事務(wù)回滾的實現(xiàn)方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07
探究Java常量本質(zhì)及三種常量池(小結(jié))
這篇文章主要介紹了探究Java常量本質(zhì)及三種常量池(小結(jié)),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-09-09
SpringBoot單元測試之數(shù)據(jù)隔離詳解
我們在寫單元測試時,有一個比較重要的要求是可以重復(fù)運行, 那么這樣就會有一個比較麻煩的問題:數(shù)據(jù)污染,所以本文為大家整理了兩個數(shù)據(jù)隔離的方式,希望對大家有所幫助2023-08-08

