读取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启动时项目修改参数的更多相关文章

  1. springboot:读取application.yml文件

    现在开发主要使用微服务框架springboot,在springboot中经常遇到读取application.yml文件的情形. 一.概述 开发过程中经常遇到要读取application.yml文件中的 ...

  2. docker-compose如何动态配置springboot项目的application.yml的配置

    假如我们再springboot的工程中有配置文件 方式1: application.properties里面存在环境变量: #配置数据库链接 spring.datasource.url = jdbc: ...

  3. omcat配置多域名站点启动时项目重复加载多次

    在tomcat中配置多个Host的时候, 出现项目重复启动多次的情况. 刚开始以为是spring boot发布项目的时候自带了一个tomcat引起的, 后来发现不是 参考了这两篇文章, 解决问题 ht ...

  4. SpringBoot在logback.xml中读取application.properties中配置的日志路径

    1.在springboot项目中使用logback记录日志,在logback.xml中配置日志存储位置时读取application.properties中配置的路径,在 logback.xml中配置引 ...

  5. 不错的linux下通用的java程序启动脚本

    不错的linux下通用的java程序启动脚本(转载) 虽然写起动shell的频率非常不高...但是每次要写都要对付一大堆的jar文件路径,新加jar包也必须要修改起动shell. 在网上找到一个挺好的 ...

  6. SpringBoot中application.yml基本配置详情

    把原有的application.properties删掉.然后 maven -X clean install,或者通过Maven Project双击clean和install(1)端口服务配置 #端口 ...

  7. SpringBoot application.yml logback.xml,多环境配置,支持 java -jar --spring.profiles.active

    趁今天有时间整理了一下 启动命令为 //开发环境 java -jar app.jar --spring.profiles.active=dev--server.port=8060 //测试环境 jav ...

  8. SpringBoot application.yml logback.xml,多环境配置,支持 java -jar --spring.profiles.active(转)

    趁今天有时间整理了一下 启动命令为 //开发环境 java -jar app.jar --spring.profiles.active=dev--server.port=8060 //测试环境 jav ...

  9. SpringBoot(二) SpringBoot核心配置文件application.yml/properties

    我们都知道在Spring中有着application.xml文件对Spring进行相关配置,通过web.xml中的contextConfigLocation指定application.xml文件所在位 ...

随机推荐

  1. PHP算法排序之快速排序、冒泡排序、选择排序、插入排序性能对比

    <?php //冒泡排序 //原理:从倒数第一个数开始,相邻的两个数比较,后面比前面的小,则交换位置,一直到比较第一个数之后则最小的会排在第一位,以此类推 function bubble_sor ...

  2. unicode utf-8 ascll编码比较

    1.字符编码     因为计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.最早的计算机在设计时采用8个比特(bit)作为一个字节(byte), 所以,一个字节能表示的最大的整数就 ...

  3. P1233 木棍加工 dp LIS

    题目描述 一堆木头棍子共有n根,每根棍子的长度和宽度都是已知的.棍子可以被一台机器一个接一个地加工.机器处理一根棍子之前需要准备时间.准备时间是这样定义的: 第一根棍子的准备时间为1分钟: 如果刚处理 ...

  4. IDEA控制台问题:java lang OutOfMemoryError:PermGen space

    PermGen space的全称是Permanent Generation space,是指内存的永久保存区域. OutOfMemoryError: PermGen space从表面上看就是内存溢出, ...

  5. Cloudera Manager的安装

    1.  cloudera manager的概念 简单来说,Cloudera Manager是一个拥有集群自动化安装.中心化管理.集群监控.报警功能的一个工具(软件),使得安装集群从几天的时间缩短在几个 ...

  6. 12306登录爬虫 session版本

    import requests import re import base64 # 定义session headers = { 'User-Agent':'Mozilla/5.0 (Windows N ...

  7. To the Max POJ - 1050 (最大子段和)

    Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous s ...

  8. CodeForces 161D Distance in Tree【树形DP】

    <题目链接> 题目大意:一颗无向无环树,有n个顶点,求其中距离为k的点对数是多少,(u,v)与(v,u)为同一点对. #include <cstdio> #include &l ...

  9. URAL 1099 Work Scheduling (一般图最大匹配) 模板题【带花树】

    <题目链接> <转载于 >>>  > 题目大意: 给出n个士兵,再给出多组士兵之间两两可以匹配的关系.已知某个士兵最多只能与一个士兵匹配.求最多能够有多少对匹 ...

  10. hdu3944

    hdu3944题目中给出的杨辉三角形的形状带有误导目的,应该转化成对称的形状再去思考这个问题分两种情况第一个是在左区从目标位置向左上方走一直走到边界,然后再向右上方一直走到起点n-k个1加上C(n-k ...