1. 程式人生 > >springboot整合quartz,報空指標錯誤,無法取得bean

springboot整合quartz,報空指標錯誤,無法取得bean

Spring有自己的Schedule定時任務,在Spring boot中使用的時候,不能動態管理JOB,於是就使用Quartz來實現。

在Spring Boot中配置Quartz:

  1. import java.io.IOException;  
  2. import java.util.Properties;  
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.beans.factory.config.PropertiesFactoryBean;  
  5. import org.springframework.context.annotation.Bean;  
  6. import org.springframework.context.annotation.Configuration;  
  7. import org.springframework.core.io.ClassPathResource;  
  8. import org.springframework.scheduling.annotation.EnableScheduling;  
  9. import org.springframework.scheduling.quartz.SchedulerFactoryBean;  
  10. @Configuration
  11. @EnableScheduling
  12. publicclass QuartzSchedule {  
  13.     @Autowired
  14.     private MyJobFactory myJobFactory;  
  15.     @Bean
  16.     public SchedulerFactoryBean schedulerFactoryBean() throws IOException {  
  17.         SchedulerFactoryBean factory = new SchedulerFactoryBean();  
  18.         factory.setOverwriteExistingJobs(true);  
  19.         // 延時啟動
  20.         factory.setStartupDelay(20
    );  
  21.         // 載入quartz資料來源配置
  22.         factory.setQuartzProperties(quartzProperties());  
  23.         // 自定義Job Factory,用於Spring注入
  24.         factory.setJobFactory(myJobFactory);  
  25.         return factory;  
  26.     }  
  27.     /** 
  28.      * 載入quartz資料來源配置 
  29.      *  
  30.      * @return 
  31.      * @throws IOException 
  32.      */
  33.     @Bean
  34.     public Properties quartzProperties() throws IOException {  
  35.         PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();  
  36.         propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));  
  37.         propertiesFactoryBean.afterPropertiesSet();  
  38.         return propertiesFactoryBean.getObject();  
  39.     }  
  40. }  

為了在JOB中使用Spring管理的Bean,需要重新定義一個Job Factory:
  1. @Component
  2. publicclass MyJobFactory extends AdaptableJobFactory {  
  3.     @Autowired
  4.     private AutowireCapableBeanFactory capableBeanFactory;  
  5.     @Override
  6.     protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {  
  7.         // 呼叫父類的方法
  8.         Object jobInstance = super.createJobInstance(bundle);  
  9.         // 進行注入
  10.         capableBeanFactory.autowireBean(jobInstance);  
  11.         return jobInstance;  
  12.     }  
  13. }<span style="font-size:14px;">  
  14. </span>  
然後在JOB中就可以使用Spring管理的Bean了
  1. publicclass MyJob implements Job, Serializable {  
  2.     privatestaticfinallong serialVersionUID = 1L;  
  3.     private Logger logger = LoggerFactory.getLogger(this.getClass());  
  4.     @Autowired
  5.     private SomeService someService;  
  6.     @Override
  7.     publicvoid execute(JobExecutionContext context) throws JobExecutionException {  
  8.         someService.doSomething();  
  9.     }  
  10. }  

相關推薦

springboot整合quartz指標錯誤無法取得bean

Spring有自己的Schedule定時任務,在Spring boot中使用的時候,不能動態管理JOB,於是就使用Quartz來實現。在Spring Boot中配置Quartz:import java.io.IOException;  import java.util.Pro

Spring AOP操作action時無法注入指標錯誤

Spring AOP操作action時無法注入,報NullPointer異常當使用Spring AOP對action層進行操作時,會出現注入失敗的問題,出現空指標異常。原因是一般struts2+spring應用中,spring的外掛只負責為action的ioc部分,但並沒有

ListView優化時控制元件行指標

 FATAL EXCEPTION: main                                   &n

stmt=conn.createStatement()指標錯誤——已解決

連線資料庫 時, stmt=conn.createStatement() 報空指標錯誤 java.lang.NullPointerException 原因是: conn=connDB.getConne

Spring Boot 單元測試,注入失敗,指標錯誤

我們在使用專案的時候,常常需求去單元測試,去測試我們寫的介面是否可以正常執行.自己在練習Spring Boot 搭建 Redis的時候進行測試.下面是測試類./** * @author jins * @date on 2018/5/6. */ @RunWith(Spri

Android中findViewById()獲取控制元件後 指標 錯誤

今天再做一個程式時,發現我使用findViewById(R.id.edit)獲取EditText時總是報空指標錯誤,我想不可能啊!! 最後從findViewById()下手,才發現原來此方法中的R.id.edit是從當前Activity或者Dialog的主佈局檔案xml中獲

Mybatis查詢查詢一個欄位的時候沒有值並且返回null指標的解決方法

select IFNULL (MAX(custid), 0) AS custId from user_work_enroll_cust where phone=#{phone}MYsql這裡我們使用IFNULL函式處理即可其他資料在SQLSERVER中我們可以這樣寫:sele

Activity傳資料到fragment並重新整理fragment指標

解決辦法: Fragment fragment = getSupportFragmentManager().findFragmentByTag(BOOK_SEARCH_LIST); if(fragment != null && !fragme

tomcat啟動一直指標錯誤

今天部署專案的時候報出這樣的錯誤: 十一月 28, 2013 2:30:02 下午 org.apache.coyote.http11.AbstractHttp11Processor process SEVERE: Error processing request java.

mybits中mapperselectOne查詢不出資料的問題selectOne指標問題select指標問題

 selectOne查詢不出資料的問題AiWechatUser iWechatUser = new AiWechatUser(); iWechatUser.setOpenid("XXX"); iWechatUser= wechartUserDao.selectOne(iWechatUser

Microsoft office2016(專業增強版) 安裝錯誤CRT(KB2999226)錯誤詳細解決方法

這個問題,實際上是跟Window Update這個服務有關。 首先右擊開啟“計算機”下面的管理屬性,點選服務,如下截圖 在"服務"中找到名稱為“Windows Update”服務,點選“停止” 然後重新啟動電腦

Eclipse中從svn檢出maven專案pom.xml錯誤相關包或類不能匯入的問題

在使用eclipse的時候,我們用svn外掛檢出專案,常常有一些問題,莫名其妙的出現,莫名其妙的又會變好了,這讓強迫症發作的我來說是個噩夢,之前看到網上的n多種解決方法,然而貌似對於我並不實用。今天自己嘗試了一種方法貌似,變好了。 第一步:當你遇到問題,自己不明其裡的時候,

springboot專案下service層注入其他service時沒有新增@Autowired專案中不就是指標

這個問題讓我折騰了快一天,因為這個方法是個通用方法,在別的service都能執行成功,就是在此service下執行不成功, 無意間發現了@autowired註解沒有加,協同開發不知道誰不小心刪除了吧。但是springboot也不報錯, try...catch後總是報空指

SpringBoot 多模組專案(module)Service自動注入(@Autowired)指標錯誤解決

 報錯資訊,這個我是截了上一部分,為了省空間下部分我就用...代替了。 java.lang.NullPointerException at com.jd.impl.UploadServiceImpl.uploadBlock(UploadServiceImpl.java:39)

指標異常算得上是一個簡單低階的錯誤吧 java.lang.NullPointerException: null

#今天早上在寫程式碼的時候突然出現了一個空指標異常,這種空針異常在實際開發過程中是完全可以避免出錯的! 實際部分程式碼如下: @PostMapping("/upload") public RestResult uploadExcelModel(HttpServ

利用反射生成例項例項內部Autowired的元件指標異常

今天在公司做一個介面以實現對mq佇列中拋異常的訊息進行訊息重發。程式碼如下: //前端傳的引數 String consumerName = "dispenseConsumer"; String methodName = methodName; String param =

ScrollView巢狀ListViewlistItem.measure(0,0);指標異常NullPointerException

1: SrollView巢狀ListView時顯示不全問題及ListView高度問題: 直接上程式碼,使用以下函式解決此問題: public void setListViewHeightOnChildren(ListView listView) {

spring依賴注入執行指標異常

java.lang.reflect.InvocationTargetException     sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     sun.reflect.NativeMethod

ssh專案中的由於spring的注入問題引起的指標錯誤

以下屬於我把弄了一天後得到的結構 注意問題: 1、保持spring中注入的,都在格式上沒有錯誤。 2、都沒有邏輯錯誤,也就是如果引用了,用到了,就必須用 3、AOP中配置事務時,把事務切入點的expr

MyEclipse 部署指標異常的解決辦法

 某些情況下MyEclipse 可能無法開啟 MyEclipse Servers檢視,試圖開啟檢視將導致NPE將被記錄。Could not create the view: An unexpected exception was thrown. java.lang.Nul