Spring Boot实战之自定义propertities
1、新建配置类Audience.java
配置前缀及路径
@ConfigurationProperties(prefix = "audience", locations = "classpath:jwt.properties")
package com.xiaofangtech.sunt.jwt;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "audience", locations = "classpath:jwt.properties")
public class Audience {
private String clientId;
private String base64Secret;
private String name;
private int expiresSecond;
public String getClientId() {
return clientId;
}
public void setClientId(String clientId) {
this.clientId = clientId;
}
public String getBase64Secret() {
return base64Secret;
}
public void setBase64Secret(String base64Secret) {
this.base64Secret = base64Secret;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getExpiresSecond() {
return expiresSecond;
}
public void setExpiresSecond(int expiresSecond) {
this.expiresSecond = expiresSecond;
}
}
2、新建properties文件,添加配置信息
audience.clientId=098f6bcd4621d373cade4e832627b4f6
audience.base64Secret=MDk4ZjZiY2Q0NjIxZDM3M2NhZGU0ZTgzMjYyN2I0ZjY=
audience.name=restapiuser
audience.expiresSecond=172800
3、在Spring Boot入口类上添加@EnableConfigurationProperties注解
@SpringBootApplication
@EnableConfigurationProperties(Audience.class)
public class SpringRestApplication {
public static void main(String[] args) {
SpringApplication.run(SpringRestApplication.class, args);
}
}
4、使用自定义的properties
添加测试接口,getaudience获取注入的内容
@Autowired
private Audience audience;
@RequestMapping("getaudience")
public Object getAudience()
{
ResultMsg resultMsg = new ResultMsg(ResultStatusCode.OK.getErrcode(), ResultStatusCode.OK.getErrmsg(), audience);
return resultMsg;
}
5、测试