spring @value 注入static 属性

来源:http://stackoverflow.com/questions/7253694/spring-how-to-inject-a-value-to-static-field

 

public class Sample {

    public static String name;

    @PostConstruct
    public void init(){
        name = privateName;
    }

    @Value("${my.name}")
    private String privateName;

    public String getPrivateName() {
        return privateName;
    }

    public void setPrivateName(String privateName) {
        this.privateName = privateName;
    }  

}

@Value("${my.name}")
public void setPrivateName(String privateName) {
    Sample.name = privateName;
} 

你可能感兴趣的:(java,spring)