spring兩種方式注入到靜態工具類里面


需要注意的是下邊的兩個方法都需要工具類創建實例的時候才會注入。所以不建議在工具類注入實例。因為一般工具類都是直接通過類來使用的。

直接上代碼

方式1:

 

//可以換成@Configuration,與@Inject配合使用
@Componentpublic 
class XXUtils { 
        //可以換成@Inject/ @Autowired
       @Resource
  private XXXProperties xxxPropertiesAutowired; 
 
  private static XXXProperties xxxProperties; 
 
       @PostConstruct 
       public void init() {
              this.xxxPropertiesAutowired = xxxProperties;
       }
}


方式2:zs

 

@Component

public class UrlUtil {

     private static RRJConfig rrjConfig;

    @Autowired
    public void setRRJConfig(RRJConfig rRJConfig) {
    UrlUtil.rrjConfig = rRJConfig;
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM