spring boot加载第三方jar包的配置文件的方法
前言
今天收到一封邮件,大概内容如下:springboot鼓励去配置化,那么怎么将第三方jar包中的xml去配置化了?
其实,这个问题,在前面的文章中也有提到,https://www.nhooo.com/article/125700.htm
下面,我们就以Quartz定时任务为例,单独对这个问题来进行说明,如何实现去配置化。
如果不使用springboot,我们配置一个简单的定时任务时,需要引入以下配置文件:
接下来的任务,就是如何将上面的xml配置文件,去配置化。
从上面的配置文件中,可以得出,我们需要配置3个实例,分别是JobDetail,JobTrigger和Scheduler。
1、首先抽取出需要在application.properties配置文件中配置的属性项,从上面的配置文件中,可以得出如下需要配置的属性项,对应的VO如下:
packagecom.chhliu.springboot.quartz.config;
importorg.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix="quartz.config")
publicclassQuartzConfigProperties{
privateStringtargetObject;
privateStringtargetMethod;
privatebooleanconcurrent;
privateStringcronExpression;
privateStringapplicationContextSchedulerContextKey;
privatebooleanwaitForJobsToCompleteOnShutdown;
……省略getter、setter方法……
}
2、在application.properties配置文件中,加入如下配置
quartz.config.targetObject=taskJob##待执行对象的名字 quartz.config.targetMethod=doJob##待执行的方法的名字 quartz.config.concurrent=false##是否并发,如果上一个定时任务还没有执行完,又被触发了,如果配置为false,则需等待上个任务执行完,才触发 quartz.config.cronExpression=0/5****?##任务触发表达式 quartz.config.applicationContextSchedulerContextKey=applicationContextKey##通过该key可以获取spring上下文 quartz.config.waitForJobsToCompleteOnShutdown=true##是否等待任务完全执行完后,再销毁线程池
3、分别实例化JobDetail,JobTrigger和Scheduler
packagecom.chhliu.springboot.quartz.entity;
importorg.quartz.Trigger;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
importorg.springframework.scheduling.quartz.CronTriggerFactoryBean;
importorg.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
importorg.springframework.scheduling.quartz.SchedulerFactoryBean;
importcom.chhliu.springboot.quartz.config.QuartzConfigProperties;
/**
*描述:将quartz的xml配置文件去配置化
*@authorchhliu
*创建时间:2017年4月11日下午7:41:21
*@version1.2.0
*/
@Configuration
publicclassQuartzConfig{
@Autowired
privateQuartzConfigPropertiesproperties;//注入属性配置文件对应的类实例
/**
*attention:
*Details:初始化JobDetail
*@authorchhliu
*创建时间:2017年4月11日下午6:17:06
*@paramtask
*@return
*MethodInvokingJobDetailFactoryBean
*@throwsClassNotFoundException
*@throwsIllegalAccessException
*@throwsInstantiationException
*/
@Bean(name="jobDetail")
publicMethodInvokingJobDetailFactoryBeandetailFactoryBean()throwsClassNotFoundException,InstantiationException,IllegalAccessException{//ScheduleTask为需要执行的任务
MethodInvokingJobDetailFactoryBeanjobDetail=newMethodInvokingJobDetailFactoryBean();
/*
*是否并发执行
*例如每5s执行一次任务,但是当前任务还没有执行完,就已经过了5s了,
*如果此处为true,则下一个任务会执行,如果此处为false,则下一个任务会等待上一个任务执行完后,再开始执行
*/
jobDetail.setConcurrent(properties.isConcurrent());
/*
*为需要执行的实体类对应的对象
*/
StringtargetObject=properties.getTargetObject();
jobDetail.setTargetBeanName(targetObject);
/*
*通过这几个配置,告诉JobDetailFactoryBean我们需要定时执行targetObject类中的properties.getTargetMethod()方法
*/
jobDetail.setTargetMethod(properties.getTargetMethod());
returnjobDetail;
}
/**
*attention:
*Details:实例化JobTrigger
*@authorchhliu
*创建时间:2017年4月11日下午7:39:14
*@paramjobDetail
*@return
*CronTriggerFactoryBean
*/
@Bean(name="jobTrigger")
publicCronTriggerFactoryBeancronJobTrigger(MethodInvokingJobDetailFactoryBeanjobDetail){
CronTriggerFactoryBeantigger=newCronTriggerFactoryBean();
tigger.setJobDetail(jobDetail.getObject());
tigger.setCronExpression(properties.getCronExpression());
returntigger;
}
/**
*attention:
*Details:实例化Scheduler
*@authorchhliu
*创建时间:2017年4月11日下午7:39:35
*@paramcronJobTrigger
*@return
*SchedulerFactoryBean
*/
@Bean(name="scheduler")
publicSchedulerFactoryBeanschedulerFactory(TriggercronJobTrigger){
SchedulerFactoryBeanbean=newSchedulerFactoryBean();
//注册触发器
bean.setTriggers(cronJobTrigger);
//通过applicationContextSchedulerContextKey属性配置获取spring上下文
bean.setApplicationContextSchedulerContextKey(properties.getApplicationContextSchedulerContextKey());
//关闭任务的时候,是否等待任务执行完毕
bean.setWaitForJobsToCompleteOnShutdown(properties.isWaitForJobsToCompleteOnShutdown());
returnbean;
}
}
4、编写需要执行的方法
packagecom.chhliu.springboot.quartz.job;
importorg.slf4j.Logger;
importorg.slf4j.LoggerFactory;
importorg.springframework.stereotype.Service;
@Service("taskJob")
publicclassTaskJob{
privatestaticfinalLoggerLOGGER=LoggerFactory.getLogger(TaskJob.class);
publicvoiddoJob(){
LOGGER.info("hellospringboot,i'mthekingoftheworld!!!");
}
}
5、测试
packagecom.chhliu.springboot.quartz;
importorg.springframework.boot.SpringApplication;
importorg.springframework.boot.autoconfigure.SpringBootApplication;
importorg.springframework.boot.context.properties.EnableConfigurationProperties;
importcom.chhliu.springboot.quartz.config.QuartzConfigProperties;
@SpringBootApplication
@EnableConfigurationProperties({QuartzConfigProperties.class})//开启配置属性支持
publicclassSpringbootQuartzApplication{
publicstaticvoidmain(String[]args){
SpringApplication.run(SpringbootQuartzApplication.class,args);
}
}
6、测试结果如下
2017-04-1119:09:35.017INFO7500---[eduler_Worker-1]c.chhliu.springboot.quartz.job.TaskJob:hellospringboot,i'mthekingoftheworld!!! 2017-04-1119:09:40.004INFO7500---[eduler_Worker-2]c.chhliu.springboot.quartz.job.TaskJob:hellospringboot,i'mthekingoftheworld!!! 2017-04-1119:09:45.004INFO7500---[eduler_Worker-3]c.chhliu.springboot.quartz.job.TaskJob:hellospringboot,i'mthekingoftheworld!!! 2017-04-1119:09:50.004INFO7500---[eduler_Worker-4]c.chhliu.springboot.quartz.job.TaskJob:hellospringboot,i'mthekingoftheworld!!! 2017-04-1119:09:55.001INFO7500---[eduler_Worker-5]c.chhliu.springboot.quartz.job.TaskJob:hellospringboot,i'mthekingoftheworld!!! 2017-04-1119:10:00.002INFO7500---[eduler_Worker-6]c.chhliu.springboot.quartz.job.TaskJob:hellospringboot,i'mthekingoftheworld!!! 2017-04-1119:10:05.001INFO7500---[eduler_Worker-7]c.chhliu.springboot.quartz.job.TaskJob:hellospringboot,i'mthekingoftheworld!!!
从上面的测试结果可以看出,任务被触发了,也得到了正确的结果。
上面的这个示例,只是一个简单的例子,但是生产上复杂的需求,原理也是类似的。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。