/** * * @PropertySource 这个注解会把 文件里面的k,v 放到环境变量中也即是 Environment * <context:property-placeholder location="classpath:/person.properties"></context:property-placeholder> * * */ @PropertySource(value = {"classpath:/person.properties"}) @Configuration public class MyUserConfig { @Bean public User getUer(){ return new User(); } } public class User { @Value("${user.name1}") private String name; @Value("31") private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "User{" + "name=‘" + name + ‘\‘‘ + ", age=" + age + ‘}‘; } } @Controller public class Mycontroller { @Autowired Environment environment; @RequestMapping("/h1") public String hello(){ System.out.println("evnvironment 中的 user.name is "+environment.getProperty("user.name1")); return "hello"; } }
@Value注解和@PropertySource 注解的使用说明
原文:https://www.cnblogs.com/gaohq/p/14856105.html