最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

spring boot加載第三方j(luò)ar包的配置文件的方法

 更新時(shí)間:2017年10月13日 10:11:50   作者:牛奮lch  
本篇文章主要介紹了spring boot加載第三方j(luò)ar包的配置文件的方法,詳細(xì)的介紹了spring boot jar包配置文件的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

前言

今天收到一封郵件,大概內(nèi)容如下:spring boot鼓勵(lì)去配置化,那么怎么將第三方j(luò)ar包中的xml去配置化了?

其實(shí),這個(gè)問(wèn)題,在前面的文章中也有提到,http://m.fzitv.net/article/125700.htm

下面,我們就以Quartz定時(shí)任務(wù)為例,單獨(dú)對(duì)這個(gè)問(wèn)題來(lái)進(jìn)行說(shuō)明,如何實(shí)現(xiàn)去配置化。

如果不使用spring boot,我們配置一個(gè)簡(jiǎn)單的定時(shí)任務(wù)時(shí),需要引入以下配置文件:

<!-- 配置需要定時(shí)執(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秒運(yùn)行一次 --> 
  <property name="cronExpression" value="0/5 * * * * ?" /> 
 </bean> 
 
 <!-- 觸發(fā)定時(shí)任務(wù) --> 
 <bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> 
  <property name="triggers"> 
   <list> 
    <ref bean="jobTrigger" /><!-- 此處可以配置多個(gè)觸發(fā)器 --> 
   </list> 
  </property> 
  <property name="applicationContextSchedulerContextKey" value="applicationContextKey" /> 
  <property name="waitForJobsToCompleteOnShutdown" value="true"></property> 
 </bean> 

接下來(lái)的任務(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ù)還沒(méi)有執(zhí)行完,又被觸發(fā)了,如果配置為false,則需等待上個(gè)任務(wù)執(zhí)行完,才觸發(fā) 
quartz.config.cronExpression=0/5 * * * * ? ## 任務(wù)觸發(fā)表達(dá)式 
quartz.config.applicationContextSchedulerContextKey=applicationContextKey ## 通過(guò)該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ù)還沒(méi)有執(zhí)行完,就已經(jīng)過(guò)了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); 
   
  /* 
   * 通過(guò)這幾個(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); 
  // 通過(guò)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!!! 

從上面的測(cè)試結(jié)果可以看出,任務(wù)被觸發(fā)了,也得到了正確的結(jié)果。

上面的這個(gè)示例,只是一個(gè)簡(jiǎn)單的例子,但是生產(chǎn)上復(fù)雜的需求,原理也是類似的。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java如何讀取csv文件并將數(shù)據(jù)放入對(duì)象中

    Java如何讀取csv文件并將數(shù)據(jù)放入對(duì)象中

    這篇文章主要介紹了Java如何讀取csv文件并將數(shù)據(jù)放入對(duì)象中的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • SpringBoot靜態(tài)資源路徑配置及主頁(yè)顯示

    SpringBoot靜態(tài)資源路徑配置及主頁(yè)顯示

    這篇文章主要介紹了SpringBoot靜態(tài)資源路徑配置及主頁(yè)顯示,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • Java日期工具類操作字符串Date和LocalDate互轉(zhuǎn)

    Java日期工具類操作字符串Date和LocalDate互轉(zhuǎn)

    這篇文章主要介紹了Java日期工具類操作字符串Date和LocalDate互轉(zhuǎn),文章首先通過(guò)需要先引入坐標(biāo)展開主題的相關(guān)內(nèi)容介紹,需要的朋友可以參一下
    2022-06-06
  • Java Web程序中利用Spring框架返回JSON格式的日期

    Java Web程序中利用Spring框架返回JSON格式的日期

    這里我們來(lái)介紹一下Java Web程序中利用Spring框架返回JSON格式的日期的方法,前提注意使用@DatetimeFormat時(shí)要引入一個(gè)類庫(kù)joda-time-版本.jar,否則會(huì)無(wú)法訪問(wèn)相應(yīng)路徑
    2016-05-05
  • java 鍵盤輸入一個(gè)數(shù),輸出數(shù)組中指定元素的示例

    java 鍵盤輸入一個(gè)數(shù),輸出數(shù)組中指定元素的示例

    今天小編就為大家分享一篇java 鍵盤輸入一個(gè)數(shù),輸出數(shù)組中指定元素的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-07-07
  • maven項(xiàng)目在實(shí)踐中的構(gòu)建管理之路的方法

    maven項(xiàng)目在實(shí)踐中的構(gòu)建管理之路的方法

    這篇文章主要介紹了maven項(xiàng)目在實(shí)踐中的構(gòu)建管理之路的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2019-05-05
  • Spring Boot 2.x基礎(chǔ)教程之使用@Scheduled實(shí)現(xiàn)定時(shí)任務(wù)的方法

    Spring Boot 2.x基礎(chǔ)教程之使用@Scheduled實(shí)現(xiàn)定時(shí)任務(wù)的方法

    在Spring Boot中編寫定時(shí)任務(wù)是非常簡(jiǎn)單的事,下面通過(guò)實(shí)例介紹如何在Spring Boot中創(chuàng)建定時(shí)任務(wù),實(shí)現(xiàn)每過(guò)5秒輸出一個(gè)當(dāng)前時(shí)間,感興趣的朋友跟隨小編一起看看吧
    2021-07-07
  • Java8新特性之新日期時(shí)間庫(kù)的使用教程

    Java8新特性之新日期時(shí)間庫(kù)的使用教程

    這篇文章主要給大家介紹了關(guān)于Java8新特性之新日期時(shí)間庫(kù)使用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • java編寫冒泡排序的完整示例

    java編寫冒泡排序的完整示例

    這篇文章主要給大家介紹了關(guān)于java編寫冒泡排序的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • Java SpringBoot的相關(guān)知識(shí)點(diǎn)詳解

    Java SpringBoot的相關(guān)知識(shí)點(diǎn)詳解

    這篇文章主要介紹了SpringBoot的相關(guān)知識(shí)點(diǎn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2021-10-10

最新評(píng)論

上杭县| 沁源县| 海口市| 大安市| 汝南县| 云和县| 新邵县| 清水县| 泉州市| 银川市| 进贤县| 沁阳市| 荥经县| 浪卡子县| 无为县| 奇台县| 探索| 棋牌| 龙胜| 富平县| 卓资县| 塔河县| 峨山| 鹿泉市| 隆德县| 西畴县| 会同县| 长岛县| 赣州市| 苗栗县| 汝城县| 西藏| 临澧县| 家居| 麻城市| 务川| 宣恩县| 虎林市| 三江| 明溪县| 昌黎县|