@Value("${wechat.url.token}")
private String token;
// 设置默认值,当注入失败取默认值
@Value("${wechat.url.token: xxxxx}")
private String token;
@Component
public class RobotUtils {
// 可以直接调用 RobotUtils.active 获取属性
public static String active;
@Value("${spring.profiles.active}")
private void setActive(String active){
RobotUtils.active = active;
}
}
@Data
@ConfigurationProperties("wechat.config")
@Component
public class WeChatConfig {
// 一次性初始化所有变量
private String tokenUrl;
private String sendUrl;
private String grantType;
}
import org.springframework.core.env.Environment;
@Resource
private Environment environment;
// 通过配置文件key获取属性
environment.getProperty(key);