使用 @Value() 注解
@Configuration
public class SystemApiConfig {
private static String account;
private static String password;
private static String appid;
@Value("${system.account}")
public void setAccouont(String account){
this.account = account;
}
@Value("${system.password}")
public void setPassword(String password){
this.password = password;
}
@Value("${system.appid}")
public void setAppid(String appid){
this.appid = appid;
}
public static String getAccount() {
return account;
}
public static String getPassword() {
return password;
}
public static String getAppid() {
return appid;
}
}
实现 InitializingBean 接口,重写 afterPropertiesSet() 方法
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class SystemApiConfig implements InitializingBean {
@Value("${system.account}")
private String account;
@Value("${system.password}")
private String password;
@Value("${system.appid}")
private String appid;
public static String ACCOUNT;
public static String PASSWORD;
public static String APPID;
@Override
public void afterPropertiesSet() throws Exception {
ACCOUNT=account;
PASSWORD=password;
APPID=appid;
}
}