Spring Boot优化了很多配置的方式方法,让简化了配置的环节,让我们能够使用极少的配置就能完成相应的功能,典型的应用便是将数据库连接信息配置在配置文件中,方便数据源以及端口的更换。
A、读取默认配置文件;
springboot的核心配置文件默认为resources根目录下的application.properties或application.yml配置文件;
application.properties文件中配置:
com.landlord.name=lin
com.landlord.sex=男
com.landlord.age=22
方法一:使用@ConfigurationProperties(prefix = "")//设置前缀
创建配置类,在@ConfigurationProperties中设置前缀,属性中不用再添加注解;
package com.landlord.test;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@ConfigurationProperties(prefix = "com.landlord")//设置前缀
@Component
public class TestProperties {
private String name;
private String sex;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
在controller中进行测试:
package com.landlord.test.controller;
import com.landlord.test.TestProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Autowired
private TestProperties testProperties;
@RequestMapping("/test01")
public String test01(){
System.out.println(testProperties.getName() + testProperties.getSex() + testProperties.getAge());
return "test01";
}
}
测试结果:
方法二:使用@Value方式
创建配置类,在属性前添加@Value注解;
用 @Value(“${xxxx}”)注解从配置文件读取值的用法
@Component
public class TestProperties2 {
@Value("${com.landlord.name}")
private String name;
@Value("${com.landlord.sex}")
private String sex;
@Value("${com.landlord.age}")
private String age;
}
测试
package com.landlord.test.controller;
import com.landlord.test.TestProperties;
import com.landlord.test.TestProperties2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Autowired
private TestProperties testProperties;
@Autowired
private TestProperties2 testProperties2;
@RequestMapping("/test01")
public String test01(){
System.out.println(testProperties.getName() + testProperties.getSex() + testProperties.getAge());
return "test01";
}
@RequestMapping("/test02")
public String test02(){
System.out.println(testProperties.getName() + testProperties.getSex() + testProperties.getAge());
return "test02";
}
}
运行效果
B、读取自定义配置文件;
test.properties文件
com.landlord.name=test
com.landlord.sex=女
com.landlord.age=212
读取自定义配置文件和上述用法一类似,在配置文件类中多添加一个注解
@PropertySource(value = "classpath:test.properties") // 配置文件路径
package com.landlord.test;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@ConfigurationProperties(prefix = "com.landlord")//设置前缀
@Component
@PropertySource(value = "classpath:test.properties") // 配置文件路径
public class TestProperties {
private String name;
private String sex;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
测试运行结果: