spring拾遺(一)——@Value注入static屬性


一般情況的下的@Value是用在非靜態方法上的,如下:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class WebServiceConstants {
    @Value("${ws_charging_url}")
    public  String  feeUrl;
}

但是對於靜態變量,我們需要采取不同的方式:

一、xml通過bean注入:沒有嘗試,理論上可行


二、給參數注入,執行set方法

public static String zhifuUrl;   
@Value("${zhifu.url}")  
public void setZhifuUrl(String url) {  
CrmUtil.zhifuUrl = url;  
}  

 

三、通過中間變量賦值

public static String zhifuUrl;   
@Value("${zhifu.url}")  
private String zhifuUrlTmp;  
  
@PostConstruct  
public void init() {  
zhifuUrl = zhifuUrlTmp;  
}  

 

 

 

 注意相關類不要忘記加@Component


免責聲明!

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



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