xml、properties、json、yaml
Springboot里面常用xx.yml
server:
port: 8080 //设置启动端口号为8080
house:
family:
name: Doe
parents:
- John
- Jane
children:
- Paul
- Mark
- Simone
address:
number: 34
street: Main Street
city: Nowheretown
zipcode: 12345
Springboot里面常用 xx.properties
server.port=8082
#session失效时间,30m表示30分钟
server.servlet.session.timeout=30m
# Maximum number of connections that the server accepts and processes at any given time.
server.tomcat.max-connections=10000
# Maximum size of the HTTP post content.
server.tomcat.max-http-post-size=2MB
server.tomcat.max-http-form-post-size=2MB
# Maximum amount of worker threads
server.tomcat.max-threads=200
官方文档配置
在resources创建配置文件pay.properties 配置文件
#有一些配置是不可以写死的,所有需要写道配置文件引入
#微信支付appid
wxpay.appid=w12324
#支付密钥
wxpay.sercret=asdasdasd
#微信支付商户号
wxpay.mechid=123123
控制类测试:
//测试配置问文件
@RestController
@RequestMapping("api/v1/test")
@PropertySource({"classpath:pay.properties"})
public class TestController {
@Value("${wxpay.appid}")
private String payAppid;
@Value("${wxpay.appid}")
private String paySecret;
@GetMapping("get_config")
public JsonData testConfig(){
Map<String,String> map=new HashMap<>();
map.put("appid",payAppid);
map.put("secret",paySecret);
return JsonData.buildSuccess(map);
}
}
测试:
配置文件:
#有一些配置是不可以写死的,所有需要写道配置文件引入
#微信支付appid
wxpay.appid=w12324
#支付密钥
wxpay.sercret=asdasdasd
#微信支付商户号
wxpay.mechid=123123
控制类测试:
//测试配置问文件
@RestController
@RequestMapping("api/v1/test")
@PropertySource({"classpath:pay.properties"})
public class TestController {
@Value("${wxpay.appid}")
private String payAppid;
@Value("${wxpay.appid}")
private String paySecret;
@Autowired //讲配置测试类注解进来
private WXConfig wxConfig;
@GetMapping("get_config")
public JsonData testConfig(){
// Map<String,String> map=new HashMap<>();
// map.put("appid",payAppid);
// map.put("secret",paySecret);
Map<String,String> map=new HashMap<>();
map.put("appid",wxConfig.getPayAppid());
map.put("Secret",wxConfig.getPaySecret());
map.put("MechId",wxConfig.getPayMechId());
return JsonData.buildSuccess(map);
}
}
postMan测试:
例子:
@Configuration
@ConfigurationProperties(prefix="test")
@PropertySource(value="classpath:resource.properties")
public class ServerConstant {
}
常见问题:
1、配置文件注入失败,Could not resolve placeholder
解决:根据springboot启动流程,会有自动扫描包没有扫描到相关注解,
默认Spring框架实现会从声明@ComponentScan所在的类的package进行扫描,来自动注入,
因此启动类最好放在根路径下面,或者指定扫描包范围
spring-boot扫描启动类对应的目录和子目录
2、注入bean的方式,属性名称和配置文件里面的key一一对应,就用加@Value 这个注解
如果不一样,就要加@value("${XXX}")