SpringBoot(⼗):读取l下配置参数信息,java-
j。。。
读取l下配置参数信息
在l⽂件内容
my:
remote-address: 192.168.1.1
yarn:
weburl: 192.168.1.1:8088/ws/v1/cluster/
security:
username: foo
roles:
- USER
- ADMIN
创建FooProperties.java⽂件,并使⽤@ConfigurationProperties注解
spring怎么读取配置@Component
@ConfigurationProperties(prefix = "my")
public class MyPorperties {
private final Yarn yarn = new Yarn();
private InetAddress remoteAddress;
private final Security security = new Security();
public InetAddress getRemoteAddress() {
return remoteAddress;
}
public void setRemoteAddress(InetAddress remoteAddress) {
}
public Security getSecurity() {
return security;
}
public Yarn getYarn() {
return yarn;
}
public static class Security {
private String userName;
private List<String> roles = new ArrayList<>(Collections.singleton("USER"));
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public List<String> getRoles() {
return roles;
}
public void setRoles(List<String> roles) {
}
@Override
public String toString() {
return "Security{" + "userName='" + userName + '\'' + ", roles=" + roles + '}';        }
}
public static class Yarn {
private String webUrl;
public String getWebUrl() {
return webUrl;
}
public void setWebUrl(String webUrl) {
this.webUrl = webUrl;
}
@Override
public String toString() {
return "Yarn [webUrl=" + webUrl + "]";
}
}
}
调⽤
@RestController
public class TestController {
@Autowired
private MyPorperties my;
@RequestMapping("/myProperties")
public Map<String, Object> getFooProperties() {
Map<String, Object> map = new HashMap<>();
map.put("remote-address", my.getRemoteAddress());
map.put("security", my.getSecurity().toString());
map.put("yarn", my.getYarn().toString());
return map;
}
}
测试
访问:localhost:8080/myProperties
参考《blog.csdn/u013887008/article/details/79368173》java -jar启动时项⽬修改参数
项⽬已经发布,在启动时可以通过传递参数修改启动参数:
java -jar \
-Dspring.profiles.active=prod \
-Dserver.port=8080 \
my-web-1.0.0-SNAPSHOT.jar
或者可以吧整个l⽂件放在jar外边,启动时指定⽂件路径:java -jar demo.jar --fig.location=路径(l)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。