[b]Spring boot Properties文件读取[/b]
@ConfigurationProperties也可以放在方法外面
参考原文:[url]http://www.cnblogs.com/softidea/p/5683522.html[/url]
@Configuration // 相当于定义XML文件
@ConfigurationProperties(prefix = "Test",locations = "classpath:test.properties")
// prefix,所需字段以什么开头的
// locations,没有时默认读取application.properties文件,设置后读取相应的properties文件
public class TestConfig {
private String id; // 需要它的set方法才可以进行Properties文件内容的引入
private String name;
@Bean(name = "testBean") // 相当于XML里配置bean,没有名字时为bean的名字(首写字母小写)
public TestBean testBean() {
TestBean client = new TestBean(id, name);
return client;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@ConfigurationProperties也可以放在方法外面
@Configuration // 相当于定义XML文件
public class TestBean3Config {
@Bean(name = "testBean3") // 相当于XML里配置bean
@ConfigurationProperties(prefix = "Test", locations = "classpath:test.properties") // 所需字段以什么开头的
public TestBean2 testBean2() {
return new TestBean2();
}
}
@Configuration // 相当于定义XML文件
//@ImportResource("classpath:dubbo-provider.xml")//包含xml文件进来
public class TestBean4Config {
@Value("${Test.}") //这种方式只能使用application.properties,不能放在别的地方
private String id;
@Value("${Test.name}")
private String name;
@Bean(name = "testBean4") // 相当于XML里配置bean
public TestBean testBean4() {
System.out.println("TestBean4Config id == " + id);
System.out.println("TestBean4Config name == " + name);
return new TestBean(id, name);
}
}
参考原文:[url]http://www.cnblogs.com/softidea/p/5683522.html[/url]