1. 程式人生 > >Springboot讀取資原始檔

Springboot讀取資原始檔

1.在resources下建立資原始檔resource.properties

com.opensource.name=imooc
com.opensource.website=www.baidu.com
com.opensource.language=java

2.建立POJO,將資原始檔中的屬性對映到實體類中

@Configuration//代表這段是會引用資原始檔的,它是個配置
@ConfigurationProperties(prefix = "com.opensource")//字首
@PropertySource(value = "classpath:resource.properties"
)//讀取類路徑下的資原始檔 public class HelloProperties { private String name; private String website; private String language; 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; } public String getLanguage() { return language; } public void setLanguage(String language) { this.language = language; } }

3.建立Controller

@RestController
public class 
HelloControl { @Autowired private HelloProperties helloProperties; @RequestMapping(value = "/hello",method = RequestMethod.GET) public String hello() { return helloProperties.getName()+helloProperties.getLanguage()+helloProperties.getWebsite(); } }