由于这里我想通过java连接linux,connection连接需要host、port、username、password及其他路径等等。不想每次修改的时候都去改源文件,所以想写在application.yml配置文件中,然后读取。

  首先需要在pom.xml中增加以下依赖,支持 @ConfigurationProperties 注解

<!-- 支持 @ConfigurationProperties 注解 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

  然后再application.yml配置文件中增加我需要的ssh常量。

ssh:
host: 192.168.8.100
port: 22
username: root
password: password

  创建一个bean接收常量,需要 @ConfigurationProperties 注解

  @Data为lombok注解省略setter和getter

  @Component 注册为bean

  @ConfigurationProperties 读取配置文件

package com.easyci.ci.entity;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; @Data
@Component
@ConfigurationProperties("ssh") //接收配置文件中“ssh”下的属性值
public class SshProperties {
private String host;
private Integer port;
private String username;
private String password;
}

测试:

@RunWith(SpringRunner.class)
@SpringBootTest
public class CiApplicationTests { @Autowired
private SshProperties sshProperties; @Test
public void contextLoads() {
Connection con = ConnectUtil.getConnect(sshProperties.getHost(),sshProperties.getUsername(),sshProperties.getPassword(),sshProperties.getPort());
System.out.println(con);
} }

结果:

  .   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.8.RELEASE) 2019-09-26 09:58:12.540 INFO 7420 --- [ main] com.easyci.ci.CiApplicationTests : Starting CiApplicationTests on DESKTOP-ANG78AB with PID 7420 (started by jxd in D:\WorkSpace\east-ci)
2019-09-26 09:58:12.540 INFO 7420 --- [ main] com.easyci.ci.CiApplicationTests : No active profile set, falling back to default profiles: default
2019-09-26 09:58:13.448 INFO 7420 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-09-26 09:58:13.660 INFO 7420 --- [ main] com.easyci.ci.CiApplicationTests : Started CiApplicationTests in 1.345 seconds (JVM running for 2.042)
服务器连接成功.
ch.ethz.ssh2.Connection@38ee7a9d
2019-09-26 09:58:14.218 INFO 7420 --- [ Thread-3] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
Disconnected from the target VM, address: '127.0.0.1:54934', transport: 'socket'

【Java】SpringBoot 中从application.yml中获取自定义常量的更多相关文章

  1. SpringBoot 从application.yml中通过@Value读取不到属性值

    package cn.exrick.xboot.mqtt; import org.eclipse.paho.client.mqttv3.*;import org.eclipse.paho.client ...

  2. ajax中的application/x-www-form-urlencoded中的使用

    ajax中的application/x-www-form-urlencoded中的使用一,HTTP上传的基本知识 在Form元素的语法中,EncType表明提交数据的格式 用 Enctype 属性指定 ...

  3. springboot获取application.yml中的配置信息

    HelloController.java package com.springbootweb.demo.controller; import com.springbootweb.demo.entity ...

  4. SpringBoot中logback.xml使用application.yml中属性

    教你如何使用 springProfile 与 springProperty 让你的logback.xml 配置显得更有逼格,当别人还在苦苦挣扎弄logback-{profile}.xml的时候 你一个 ...

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

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

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

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

  7. springboot:读取application.yml文件

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

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

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

  9. springboot加载application.yml文件null

    话不多说,直接上代码 本人项目为maven项目 以下是项目结构 pom.xml文件 <?xml version="1.0" encoding="UTF-8" ...

随机推荐

  1. 使用docker快速搭建本地环境

    在平时的开发中工作中,环境的搭建其实一直都是一个很麻烦的事情 特别是现在,系统越来越复杂,所需要连接的一些中间件也越来越多. 然而要顺利的安装好这些中间件也是一个比较费时费力的工作. 俗话说" ...

  2. MyBatis的parameterType传入参数类型

    在mybatis映射接口的配置中,有select,insert,update,delete等元素都提到了parameterType的用法,parameterType为输入参数,在配置的时候,配置相应的 ...

  3. Spring框架入门之AOP

    Spring框架入门之AOP 一.Spring AOP简单介绍 AOP AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented ...

  4. codeforces 755D. PolandBall and Polygon(线段树+思维)

    题目链接:http://codeforces.com/contest/755/problem/D 题意:一个n边形,从1号点开始,每次走到x+k的位置如果x+k>n则到x+k-n的位置,问每次留 ...

  5. POJ 2230 Watchcow 欧拉图

    Watchcow Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 8800   Accepted: 3832   Specia ...

  6. 2014 北京区域赛 dp

    Matt has N friends. They are playing a game together. Each of Matt’s friends has a magic number. In ...

  7. .NET Core CSharp 中级篇2-8 特性标签

    .NET Core CSharp 中级篇2-8 本节内容为特性标签 简介 标签Attribute是一个非常重要的技术,你可以使用Attribute技术优化精简你的代码.特性标签可以运用在程序集,模块, ...

  8. react页面内嵌微信二维码 和 自定义样式 以及 微信网页共用unionId问题

    在react页面内嵌“微信二维码”,实现PC端通过微信扫码进行登录.首先去微信开放平台注册一个账号,创建一个网站应用,提交网站备案审核,获取appid和appsecret:其他开发流程根据微信文档来进 ...

  9. JS-特效 ~ 04. client对象、网页可视区域的宽高、client / offset / scroll 三大家族的区别、冒泡事件、事件委托、获取内嵌式和外链式属性getStyle(ele,attr) ;、缓动动画封装

    知识点: 模拟滚动条的解除事件问题 : event内置对象,包含 了大量事件: page兼容性: pageX || clientX + scool().top  : if (true === a)tr ...

  10. spring aop 的一个思考

    问题: spring  aop 默认使用jdk代理织入. 也就是我们常这样配置:<aop:aspectj-autoproxy /> 通过aop命名空间的<aop:aspectj-au ...