- 在核心配置文件中定义有前缀的key
school.name=bjdaxue
school.websit=http://tshuau.com
person.name=shanshan
person.websit=http://person.com
- 在Appplication入口类的同级或子目录下创建对应前缀的类,如:School、Person,并在类名上面假如 @Component//将此类交给spring容器进行管理
@ConfigurationProperties(prefix = “school”)//将此类设置为配置类,然后设置对应的属性
@Component
@ConfigurationProperties(prefix = "school")
public class School {
private String name;
private String websit;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWebsit() {
return websit;
}
public void setWebsit(String websit) {
this.websit = websit;
}
}
- 在Controller中注入
@Autowired
private School school;
@Autowired
private Person person;
- 最后就可以使用该类获取对应的功能
@RequestMapping(value = "/say")
public @ResponseBody String say(){
return "school.name"+school.getName()+"------------schoolwebsit"+school.getWebsit()
+"----person.name"+person.getName()+"---------person.websit"+person.getWebsit();
}