1. 程式人生 > >使用spring的@Value註解如何注入static變數

使用spring的@Value註解如何注入static變數

兩種實現方式:

//[email protected]方式
@Value("${check_rule}")
private String checkRule;
@Value("${id_type}")
private String idType;
@PostConstruct
private void init() {
    initRandom=random;
}

//2.在set方法使用@Value即可, class需要加 @Component等註解, 這樣spring才能掃描到, <context:component-scan base-package="com.coffee.app" />, 還需要注意的一點是 包掃描要載入spring-mvc.xml中而不是applicationContext.xml
private static String integraionUrl;

@Value("${integraion.url}")
public void setIntegrationUrl(String param) {
    this.integraionUrl= param;
}