Servlet無法自動注入配置檔案值的解決

2021-10-08 11:34:22 字數 808 閱讀 6383

之前用spring的controller和service可以自動注入,但是在servlet裡卻不可以。

於是手動注入一下。

大概思路是根據相對路徑,拿到web裡絕對路徑的位址,然後用檔案輸入流讀取。

之後用properities類解析一下,就可以拿到了。

properties prop = new properties();

string path = thread.currentthread().getcontextclassloader().getresource("").getpath();

inputstream is = new fileinputstream(path + "/config/db/dev/buffalo.properties");

prop.load(is);

string topic = prop.getproperty("jmq.topic");

string user = prop.getproperty("jmq.user");

string address = prop.getproperty("jmq.address");

string password = prop.getproperty("jmq.password");

配置檔案:

效果:

Spring Quartz無法自動注入bean問題

我們有時需要執行一些定時任務 如資料批處理 比較常用的技術框架有spring quartz中。無奈此方式有個問題 spring bean無法自動注入。成員變數新增註解 autowired 然後在方法中 如例子中的querytraderno方法 新增以下 自動注入成員變數實現類 springbeana...

Servlet中Autowired 注入為空解決。

1.部署weblogic時候,springboot原有的方式報,不支援型別錯誤,required multipartfile parameter file is not present。改為servlet上傳,引入commons fileupload.jar apache commons commo...

SpringBoot bean無法注入

description field userp in com.springboot.controller.usercontroller required a bean of type com.springboot.model.userproperties that could not be foun...