午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁技術文章
文章詳情頁

java - spring-boot怎樣優雅得插入一個后臺線程?

瀏覽:185日期:2024-02-07 09:57:52

問題描述

我有一個守護,可它需要插入數據到數據庫.不知道怎樣注入Bean服務,所以目前是這樣的:

public static void main(String[] args) { Thread daemon=new Thread(new DaemonRun()); daemon.setDaemon(true); daemon.start(); SpringApplication.run(Application.class, args); } ....public class DaemonRun implements Runnable { private DataService dataService; public synchronized DataService getDataService(){if(dataService==null)dataService=(DataService)SpringApplicationContextHolder.getSpringBean('dataService');return dataService; }

有沒有辦法讓DataService 自動注入DaemonRun同時DaemonRun又開機運行在一個獨立線程里呢?

問題解答

回答1:

不好意思各位,我自己找到方法了.:http://stackoverflow.com/ques...

根據回答的方法.改為@Componentclass ThreadRun implements DisposableBean.....然后在構造里啟動線程,destroy里關閉線程,而且能用到自動注入

回答2:

你的意思是想進行接口的bean自動注入吧? 你可以參考spring關于抽象類的bean或接口對象的注入創建一個抽象類DataService implements Runnable 進行注入。然后extend它。因為springbean 的生命周期是在beanFactory創建的時候就創建完成,你的對象是創建的時候才進行對象需要注入,這點與spring的概念沖突。

以下摘自stackoverflow,

Mark the abstract base class definition as abstract by using the abstract attribute , and in the concrete class definition , make the parent attribute be the name of the abstract class ’s bean nameSomething like this:<bean abstract='true' class='pacakge1.AbstractBaseClass'> <property name='mailserver' value='DefaultMailServer'/></bean><bean parent='abstractBaseClass'> <!--Override the value of the abstract based class if necessary--> <property name='mailserver' value='AnotherMailServer'/></bean>回答3:

使用自動注入,把scope配置成prototype試試吧。

回答4:

這個線程是做什么用的?

標簽: java
相關文章:
主站蜘蛛池模板: 久久午夜精品视频 | 欧洲影院 | 亚洲日日日 | 欧美高清a | 欧美a级成人淫片免费看 | 成人免费视频大全 | 黄色片在线免费观看视频 | 超碰九九 | 国产麻豆免费视频 | 免费av网站观看 | 国产一区二区视频免费 | 婷婷色亚洲 | 五月天伊人 | 亚洲成人av一区二区三区 | www.国产精品.com| 婷婷色吧| 国产白浆视频 | 国产欧美精品一区二区三区 | 最新国产精品 | 日本黄色中文字幕 | 亚洲v | 国产精品91在线 | 99re这里都是精品 | 亚洲色图色 | 三级国产三级在线 | 超碰在线cao | 国产va在线观看 | 久久国产视频一区 | 乳色吐息樱花 | www国产在线 | 黄色成年视频 | 日本欧美一区二区三区 | 欧美精品色 | 色婷婷国产精品久久包臀 | 国产影视一区 | 日韩久久中文字幕 | 成人高清视频在线观看 | 中文字幕国产精品 | 久久伊人精品 | 成人国产精品久久 | 色视频在线免费观看 |