工具類中使用Spring注入的類

2021-08-28 07:12:04 字數 326 閱讀 2586

@component

public class excelutil

/*** 賦值方法

* */

public static void do()

}@autowired:作用於構建器、屬性、方法。按bytype自動注入。(@resource預設按 byname自動注入) 

@postconstruct:被@postconstruct修飾的方法會在伺服器載入servle的時候執行,並且只會被伺服器執行一次。postconstruct在建構函式之後執行,init()方法之前執行。(predestroy()方法在destroy()方法執行執行之後執行)

在普通類中使用Spring注入

在編寫工具類 提供方法的時候由於要注入service層,在編寫的時候發現service層類注入失敗,由於普通類不是通過spring進行管理的,所有注入service會失敗。所以可以採用下面的方法。springtool需要加到spring配置中,如果採用自動掃瞄需要將springtool宣告為元件。新...

Web Filter中使用Spring注入bean

b 方法一 b web.xml配置乙個 delegatingfilterproxy org.springframework.web.filter.delegatingfilterproxy targetbeanname myfilter 自己過濾器的名字 targetfilterlifecycle ...

工具類中注入Spring物件

因為日常工作中肯定會使用過到工具類 裡面必然會有靜態方法 但是spring 是無法直接注入靜態方法的 需要一些額外的工作 第一種 宣告為spring元件 component public class utils 第二種 component public class staticutils 因為工具方...