spring boot加载第三方jar包的配置文件的方法
作者:牛奋lch 发布时间:2023-03-02 22:45:13
标签:spring,boot,jar包
前言
今天收到一封邮件,大概内容如下:spring boot鼓励去配置化,那么怎么将第三方jar包中的xml去配置化了?
其实,这个问题,在前面的文章中也有提到,https://www.jb51.net/article/125700.htm
下面,我们就以Quartz定时任务为例,单独对这个问题来进行说明,如何实现去配置化。
如果不使用spring boot,我们配置一个简单的定时任务时,需要引入以下配置文件:
<!-- 配置需要定时执行的任务类以及方法 -->
<bean id="doJob"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 指定任务类 -->
<property name="targetObject" ref="schedulerTask" />
<!-- 指定任务执行的方法 -->
<property name="targetMethod" value="doTask" />
<property name="concurrent" value="false"></property>
</bean>
<!-- 配置触发器 -->
<bean id="jobTrigger"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="doJob" />
<!-- 每5秒运行一次 -->
<property name="cronExpression" value="0/5 * * * * ?" />
</bean>
<!-- 触发定时任务 -->
<bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="jobTrigger" /><!-- 此处可以配置多个触发器 -->
</list>
</property>
<property name="applicationContextSchedulerContextKey" value="applicationContextKey" />
<property name="waitForJobsToCompleteOnShutdown" value="true"></property>
</bean>
接下来的任务,就是如何将上面的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;
/**
* 描述:将quartz的xml配置文件去配置化
* @author chhliu
* 创建时间:2017年4月11日 下午7:41:21
* @version 1.2.0
*/
@Configuration
public class QuartzConfig {
@Autowired
private QuartzConfigProperties properties; // 注入属性配置文件对应的类实例
/**
* attention:
* Details:初始化JobDetail
* @author chhliu
* 创建时间: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为需要执行的任务
MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();
/*
* 是否并发执行
* 例如每5s执行一次任务,但是当前任务还没有执行完,就已经过了5s了,
* 如果此处为true,则下一个任务会执行,如果此处为false,则下一个任务会等待上一个任务执行完后,再开始执行
*/
jobDetail.setConcurrent(properties.isConcurrent());
/*
* 为需要执行的实体类对应的对象
*/
String targetObject = properties.getTargetObject();
jobDetail.setTargetBeanName(targetObject);
/*
* 通过这几个配置,告诉JobDetailFactoryBean我们需要定时执行targetObject类中的properties.getTargetMethod()方法
*/
jobDetail.setTargetMethod(properties.getTargetMethod());
return jobDetail;
}
/**
* attention:
* Details:实例化JobTrigger
* @author chhliu
* 创建时间: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
* 创建时间:2017年4月11日 下午7:39:35
* @param cronJobTrigger
* @return
* SchedulerFactoryBean
*/
@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!!!
从上面的测试结果可以看出,任务被触发了,也得到了正确的结果。
上面的这个示例,只是一个简单的例子,但是生产上复杂的需求,原理也是类似的。
来源:http://blog.csdn.net/liuchuanhong1/article/details/70105193


猜你喜欢
- 一、什么是备忘录模式定义:在不破坏封闭的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可将该对象恢复到原先保存的状态
- 一、基本概念C#只有两种数据类型:值类型和引用类型值类型在线程栈分配空间,引用类型在托管堆分配空间值类型转为引用类型称成为装箱,引用类型转为
- private void showPopupView() { if (mPopupWindow ==
- summarydetail传统的Spring项目会有很多的配置文件,比如我们要使用Redis,一般除了对应的依赖的jar包我们还需要在app
- 每次看IOS上的应用,应用中状态栏的颜色总能与应用标题栏颜色保持一致,用户体验很不错,对于这种效果,像我这种好奇心强的人就会去看看那安卓是否
- SpringBoot starter用了springboot 那么久了居然都还没自定义过starter,想想都觉得羞愧,所以今天来玩一下。S
- 背景:最近需要用到人脸识别,但又不花钱使用现有的第三方人脸识别接口,为此使用opencv结合java进行人脸识别(ps:opencv是开源的
- 本文实例为大家分享了Android自定义View之组合控件,仿电商app顶部栏的相关代码,供大家参考,具体内容如下效果图:分析:左右两边可以
- 本文实例讲述了C#实现集合转换成json格式数据的方法。分享给大家供大家参考,具体如下:/// <summary>/// dat
- 1.拉取centos镜像docker pull centos:72.基于拉取到的镜像运行一个容器docker run -it --name
- Android N 中推出了多窗口支持,项目要求适配多窗口模式,记录一下。1.生命周期:对于完全没有适配多窗口的APP来说,当启用多窗口模式
- 本文实例分析了C#实现的24点游戏。分享给大家供大家参考。具体如下:1. 24点游戏规则及算法规则:给出4个自然数,找出能够求出24的四则运
- 上一篇 主要介绍了如何通过蓝牙连接到打印机。这一篇,我们就介绍如何向打印机发送打印指令,来打印字符和图片。1. 构造输出流首先要明确一点,就
- 前面,学了物体的移动功能,现在来学一下C#实现鼠标控制摄像机(视角)移动。代码如下:C#脚本(在Unity 5.5.1 下能运行):usin
- MyBatis查询数据赋值给List集合数据缺少今天在使用MyBatis查询数据时,发现查出来的数据和List集合的大小不一致,如下图所示,
- 前言在移动互联网浪潮中,联网APP已经把单机拍死在沙滩上,很多公司都希望自家应用能够有一套帐号系统,可是许多用户却并不一定买账:
- Android WebView常见问题解决方案汇总:就目前而言,如何应对版本的频繁更新呢,又如何灵活多变地展示我们的界面呢,这又涉及到了we
- 在 Flutter 中使用图片是最基础能力之一。作为春节开工后的第一篇文章,17 做了精心准备,满满的都是干货!本文介绍如何在 Flutte
- 介绍今天主要分享一下 kafka 的 rebalance,在 kafka 中,rebalance 是一个十分重要的概念,很多时候引发的一些问
- 一、Maven简介1. 什么是MavenMaven:是Apache提供的免费开源的项目管理工具。它提供了一个项目对象模型(pom.xml)、