springboot整合quartz,報空指標錯誤,無法取得bean
Spring有自己的Schedule定時任務,在Spring boot中使用的時候,不能動態管理JOB,於是就使用Quartz來實現。
在Spring Boot中配置Quartz:
- import java.io.IOException;
- import java.util.Properties;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.config.PropertiesFactoryBean;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.core.io.ClassPathResource;
- import org.springframework.scheduling.annotation.EnableScheduling;
- import org.springframework.scheduling.quartz.SchedulerFactoryBean;
- @Configuration
- @EnableScheduling
- publicclass QuartzSchedule {
- @Autowired
- private MyJobFactory myJobFactory;
- @Bean
- public SchedulerFactoryBean schedulerFactoryBean() throws IOException {
- SchedulerFactoryBean factory = new SchedulerFactoryBean();
- factory.setOverwriteExistingJobs(true);
- // 延時啟動
- factory.setStartupDelay(20
- // 載入quartz資料來源配置
- factory.setQuartzProperties(quartzProperties());
- // 自定義Job Factory,用於Spring注入
- factory.setJobFactory(myJobFactory);
- return factory;
- }
- /**
- * 載入quartz資料來源配置
- *
- * @return
- * @throws IOException
- */
- @Bean
- public Properties quartzProperties() throws IOException {
- PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
- propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
- propertiesFactoryBean.afterPropertiesSet();
- return propertiesFactoryBean.getObject();
- }
- }
為了在JOB中使用Spring管理的Bean,需要重新定義一個Job Factory:
- @Component
- publicclass MyJobFactory extends AdaptableJobFactory {
- @Autowired
- private AutowireCapableBeanFactory capableBeanFactory;
- @Override
- protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
- // 呼叫父類的方法
- Object jobInstance = super.createJobInstance(bundle);
- // 進行注入
- capableBeanFactory.autowireBean(jobInstance);
- return jobInstance;
- }
- }<span style="font-size:14px;">
- </span>
- publicclass MyJob implements Job, Serializable {
- privatestaticfinallong serialVersionUID = 1L;
- private Logger logger = LoggerFactory.getLogger(this.getClass());
- @Autowired
- private SomeService someService;
- @Override
- publicvoid execute(JobExecutionContext context) throws JobExecutionException {
- someService.doSomething();
- }
- }
相關推薦
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中mapper,selectOne查詢不出資料的問題,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巢狀ListView,listItem.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