SpringBoot(十):读取application.yml下配置参数信息,java -jar启动时项目修改参数
读取application.yml下配置参数信息
在application.yml
文件内容
my:
remote-address: 192.168.1.1
yarn:
weburl: http://192.168.1.1:8088/ws/v1/cluster/
security:
username: foo
roles:
- USER
- ADMIN
创建FooProperties.java
文件,并使用@ConfigurationProperties
注解
@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) {
this.remoteAddress = 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) {
this.roles = 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;
}
}
测试
访问:http://localhost:8080/myProperties
参考《https://blog.csdn.net/u013887008/article/details/79368173》
java -jar启动时项目修改参数
项目已经发布,在启动时可以通过传递参数修改启动参数:
java -jar \
-Dspring.profiles.active=prod \
-Dserver.port=8080 \
my-web-1.0.0-SNAPSHOT.jar
或者可以吧整个application.yml文件放在jar外边,启动时指定文件路径:
java -jar demo.jar --Dspring.config.location=路径(application.yml)
SpringBoot(十):读取application.yml下配置参数信息,java -jar启动时项目修改参数的更多相关文章
- springboot:读取application.yml文件
现在开发主要使用微服务框架springboot,在springboot中经常遇到读取application.yml文件的情形. 一.概述 开发过程中经常遇到要读取application.yml文件中的 ...
- docker-compose如何动态配置springboot项目的application.yml的配置
假如我们再springboot的工程中有配置文件 方式1: application.properties里面存在环境变量: #配置数据库链接 spring.datasource.url = jdbc: ...
- omcat配置多域名站点启动时项目重复加载多次
在tomcat中配置多个Host的时候, 出现项目重复启动多次的情况. 刚开始以为是spring boot发布项目的时候自带了一个tomcat引起的, 后来发现不是 参考了这两篇文章, 解决问题 ht ...
- SpringBoot在logback.xml中读取application.properties中配置的日志路径
1.在springboot项目中使用logback记录日志,在logback.xml中配置日志存储位置时读取application.properties中配置的路径,在 logback.xml中配置引 ...
- 不错的linux下通用的java程序启动脚本
不错的linux下通用的java程序启动脚本(转载) 虽然写起动shell的频率非常不高...但是每次要写都要对付一大堆的jar文件路径,新加jar包也必须要修改起动shell. 在网上找到一个挺好的 ...
- SpringBoot中application.yml基本配置详情
把原有的application.properties删掉.然后 maven -X clean install,或者通过Maven Project双击clean和install(1)端口服务配置 #端口 ...
- SpringBoot application.yml logback.xml,多环境配置,支持 java -jar --spring.profiles.active
趁今天有时间整理了一下 启动命令为 //开发环境 java -jar app.jar --spring.profiles.active=dev--server.port=8060 //测试环境 jav ...
- SpringBoot application.yml logback.xml,多环境配置,支持 java -jar --spring.profiles.active(转)
趁今天有时间整理了一下 启动命令为 //开发环境 java -jar app.jar --spring.profiles.active=dev--server.port=8060 //测试环境 jav ...
- SpringBoot(二) SpringBoot核心配置文件application.yml/properties
我们都知道在Spring中有着application.xml文件对Spring进行相关配置,通过web.xml中的contextConfigLocation指定application.xml文件所在位 ...
随机推荐
- User模型扩展和自定义
参考如下: django文档参考 django signal使用总结 django 信号注册 django信号问题1 django oneTooneFiled 1. django 自定义用户u ...
- 移动端上拉加载下拉刷新插件-mescroll.js插件
官网地址是:http://www.mescroll.com // 初始化mescroll function initMeScroll() { //创建MeScroll对象,内部已默认开启下拉刷新,自动 ...
- sql 分隔字符串函数
USE [tms]GO/****** Object: UserDefinedFunction [dbo].[fn_ConvertListToTable_Sort] Script Date: 2017/ ...
- Redis Pubsub命令用法
一.什么是pub/sub及实现Pub/Sub功能(means Publish, Subscribe)即发布及订阅功能. Redis通过publish和subscribe命令实现订阅和发布的功能. 订阅 ...
- IDEA添加源码包
1.在项目中选中左上角的File--->Project Structure 2.选择需要添加的源码包 3.源码已经加入
- Linux常用命令大全(转)
(转)Linux常用命令大全(非常全!!!) 最近都在和Linux打交道,感觉还不错.我觉得Linux相比windows比较麻烦的就是很多东西都要用命令来控制,当然,这也是很多人喜欢linux的原因, ...
- 按键排序JavaScript对象
问题描述 我需要按键排序JavaScript对象. 因此如下: { 'b' : 'asdsad', 'c' : 'masdas', 'a' : 'dsfdsfsdf' } 会成为: { 'a' : ' ...
- css的基本定位机制
分为三种:普通流.浮动.绝对定位 普通流:默认,html文档的排列顺序块级从上到下(垂直距离由margin-top.margin-bottom决定):行内元素在一行中从左到右(由margin-left ...
- NOIP2017 题解(给自己看的) --有坑要填
目录 D1T1精妙证明: D1T3 D2T2 几道水题就不写了.... D1T1精妙证明: 把ax+by = z 的z按照模a剩余系分类 由于\((a,b)=1\)所以对于每个\(k\in[0, a) ...
- 2019-1-11 SQL语句汇总——聚合函数、分组、子查询及组合查询