server.port=8081
school.name=bjpowernode
school.website=www.bjpowernode.com
city.name=beijing
city.website=www.beijing.com
package com.bjpowernode.springboot.model;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component //将此类加载到spring容器中
@ConfigurationProperties(prefix = "school") //此school为配置文件中key值的前缀
public class School {
private String name;
private String website;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWebsite() {
return website;
}
public void setWebsite(String website) {
this.website = website;
}
}
package com.bjpowernode.springboot.model;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component //将此类加载到spring容器中
@ConfigurationProperties(prefix = "city")
public class City {
private String name;
private String website;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWebsite() {
return website;
}
public void setWebsite(String website) {
this.website = website;
}
}
package com.bjpowernode.springboot.web;
import com.bjpowernode.springboot.model.City;
import com.bjpowernode.springboot.model.School;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class IndexController {
@Autowired
private School school;
@Autowired
private City city;
@RequestMapping(value = "/school")
public @ResponseBody Object myschool(){
return "school.name="+school.getName()+", school.website="+school.getWebsite();
}
@RequestMapping(value = "/city")
public @ResponseBody Object mycity(){
return "city.name="+city.getName()+", city.website="+city.getWebsite();
}
}

