栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring boot加载第三方jar包的配置文件的方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring boot加载第三方jar包的配置文件的方法

前言

今天收到一封邮件,大概内容如下:spring boot鼓励去配置化,那么怎么将第三方jar包中的xml去配置化了?

其实,这个问题,在前面的文章中也有提到,https://www.jb51.net/article/125700.htm

下面,我们就以Quartz定时任务为例,单独对这个问题来进行说明,如何实现去配置化。

如果不使用spring boot,我们配置一个简单的定时任务时,需要引入以下配置文件:

 
  
   
   
   
   
   
  
  
  
  
   
   
   
  
 
  
  
   
    
     
    
   
   
   
  

接下来的任务,就是如何将上面的xml配置文件,去配置化。

从上面的配置文件中,可以得出,我们需要配置3个实例,分别是JobDetail,JobTrigger和Scheduler。

1、首先抽取出需要在application.properties配置文件中配置的属性项,从上面的配置文件中,可以得出如下需要配置的属性项,对应的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 ## 待执行对象的名字 
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 

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; 
 
 
@Configuration 
public class QuartzConfig { 
  
 @Autowired 
 private QuartzConfigProperties properties; // 注入属性配置文件对应的类实例 
  
  
 @Bean(name = "jobDetail") 
 public MethodInvokingJobDetailFactoryBean detailFactoryBean() throws ClassNotFoundException, InstantiationException, IllegalAccessException {// ScheduleTask为需要执行的任务 
  MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean(); 
   
  jobDetail.setConcurrent(properties.isConcurrent()); 
   
   
  String targetObject = properties.getTargetObject(); 
  jobDetail.setTargetBeanName(targetObject); 
   
   
  jobDetail.setTargetMethod(properties.getTargetMethod()); 
  return jobDetail; 
 } 
  
  
 @Bean(name = "jobTrigger") 
 public CronTriggerFactoryBean cronJobTrigger(MethodInvokingJobDetailFactoryBean jobDetail) { 
  CronTriggerFactoryBean tigger = new CronTriggerFactoryBean(); 
  tigger.setJobDetail(jobDetail.getObject()); 
  tigger.setCronexpression(properties.getCronexpression()); 
  return tigger; 
 
 } 
  
  
 @Bean(name = "scheduler") 
 public SchedulerFactoryBean schedulerFactory(Trigger cronJobTrigger) { 
  SchedulerFactoryBean bean = new SchedulerFactoryBean(); 
  // 注册触发器 
  bean.setTriggers(cronJobTrigger); 
  // 通过applicationContextSchedulerContextKey属性配置获取spring上下文 
  bean.setApplicationContextSchedulerContextKey(properties.getApplicationContextSchedulerContextKey()); 
  // 关闭任务的时候,是否等待任务执行完毕 
  bean.setWaitForJobsToCompleteonShutdown(properties.isWaitForJobsToCompleteonShutdown()); 
  return bean; 
 } 
} 

4、编写需要执行的方法 

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、测试结果如下 

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!!! 

从上面的测试结果可以看出,任务被触发了,也得到了正确的结果。

上面的这个示例,只是一个简单的例子,但是生产上复杂的需求,原理也是类似的。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/143819.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号