启动类添加注解@EnableConfigurationProperties

import jnetman.session.SnmpPref;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.scheduling.annotation.EnableScheduling;
@EnableCaching
@EnableScheduling
@SpringBootApplication
@EnableConfigurationProperties({ SnmpPref.class })
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

application.yml

snmp:
job:
cronExpr: 0/30 * * * * ?
config:
v3User: root
password: xxx
privacyDES: bbb
port: 161
trapsPort: 162
timeout: 3000
maxRetries: 3
isSnmp4JLogEnabled: true
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; /***
* Snmp协议相关配置参数
*/
@ConfigurationProperties(prefix = "snmp.config")
public class SnmpPref { public static String v3User; public static String password; public static String privacyDES; public static int port; public static int trapsPort; public static int timeout; public static int maxRetries; public static boolean isSnmp4JLogEnabled; @Value("${snmp.config.v3User}")
public void setV3User(String v3User) {
SnmpPref.v3User = v3User;
} @Value("${snmp.config.password}")
public void setPassword(String password) {
SnmpPref.password = password;
} @Value("${snmp.config.privacyDES}")
public void setPrivacyDES(String privacyDES) {
SnmpPref.privacyDES = privacyDES;
} @Value("${snmp.config.port}")
public void setPort(int port) {
SnmpPref.port = port;
} @Value("${snmp.config.trapsPort}")
public void setTrapsPort(int trapsPort) {
SnmpPref.trapsPort = trapsPort;
} @Value("${snmp.config.timeout}")
public void setTimeout(int timeout) {
SnmpPref.timeout = timeout;
} @Value("${snmp.config.maxRetries}")
public void setMaxRetries(int maxRetries) {
SnmpPref.maxRetries = maxRetries;
} @Value("${snmp.config.isSnmp4JLogEnabled}")
public void setIsSnmp4JLogEnabled(boolean isSnmp4JLogEnabled) {
SnmpPref.isSnmp4JLogEnabled = isSnmp4JLogEnabled;
} public static String getUser() {
return v3User;
} public static String getPassword() {
return password;
} public static String getPrivacyDES()
{
return privacyDES;
} public static int getPort() {
return port;
} public static int getTrapsPort() {
return trapsPort;
} public static int getTimeout() {
return timeout;
} public static int getMaxRetries() {
return maxRetries;
} public static boolean isSnmp4jLogEnabled() {
return isSnmp4JLogEnabled;
}
}

使用方法:

...
this(targetDevice,SnmpPref.getUser(),SnmpPref.getPassword(),SnmpPref.getPrivacyDES());
...

springboot中静态属性/静态方法从YAML(yml)读取配置属性的更多相关文章

  1. IntelliJ IDEA+SpringBoot中静态资源访问路径陷阱:静态资源访问404

    IntelliJ IDEA+SpringBoot中静态资源访问路径陷阱:静态资源访问404 .embody{ padding:10px 10px 10px; margin:0 -20px; borde ...

  2. SpringBoot:静态资源映射、定制404、配置icon

    目录 静态资源映射规则 定制首页 定制错误页面 配置 icon 静态资源映射规则.定制首页.定制404页面.配置网站的图标 静态资源映射规则 SpringBoot中对于静态资源(css,js,img. ...

  3. Springboot中静态资源和拦截器处理(踩了坑)

    背景: 在项目中我使用了自定义的Filter 这时候过滤了很多路径,当然对静态资源我是直接放过去的,但是,还是出现了静态资源没办法访问到springboot默认的文件夹中得文件 说下默认映射的文件夹有 ...

  4. 8 — springboot中静态资源处理方式 - 前后端分离 这没屁用

    7中说了thymeleaf,哪还有一个目录是static 那么就来研究一下静态资源 静态资源,springboot底层是怎么去装配的,都在WebMvcAutoConfiguration有答案,去看一下 ...

  5. SpringBoot中maven打包,启动报没有主清单属性

    有时候会出现这种情况,看一下项目的pom中是否有这个插件配置,没有的话需要引入. <build> <plugins> <plugin> <groupId> ...

  6. 关于SpringBoot中静态资源访问的问题

    第一种方式 : 放在src/main/webapp目录下 第二种方式:放在classpath下(网页存放于static目录下, 默认的"/"指向的是~/resouces/stati ...

  7. 【spring boot】SpringBoot初学(2.2)– SpEL表达式读取properties属性到Java对象

    前言 github: https://github.com/vergilyn/SpringBootDemo 代码位置:(注意测试方法在,test下的SpelValueApplicationTest.c ...

  8. Springboot 中的配置文件

    Spring Boot提供了两种常用的配置文件,分别是properties文件和yml文件.他们的作用都是修改Spring Boot自动配置的默认值. 技术:yaml.properties语法,Con ...

  9. 【2.0】SpringBoot多环境yml文件配置

    一.使用Spring Boot Profiles 1. 使用yml文件 首先,我们先创建一个名为 application.yml的属性文件,如下: server: port: 8080 my: nam ...

随机推荐

  1. windows 快捷键收集

    1. 放大镜 windows徽标 + "+“ 2. 直接显示桌面 windows徽标 + D 3. 收起所有窗口 windows徽标 + M 4. 浏览器中恢复之前关闭的页面 Ctrl + ...

  2. “美登杯”上海市高校大学生程序设计 C. 小花梨判连通 (并查集+map)

    Problem C C . 小 花梨 判连通 时间限制:2000ms 空间限制:512MB Description 小花梨给出

  3. socket链接的关闭连接与close和shutdown的区别

    TCP主动关闭连接 appl: close(), --> FIN FIN_WAIT_1 //主动关闭socket方,调用close关闭socket,发FIN <-- ACK FIN_WAI ...

  4. c# 获取api 数据

    private string GetDataFromServerApi(string url, string body) { string str = ""; try { Http ...

  5. HTML 获取class里的多个值 和 dataset的使用

    属性:classList var x = event.target; console.log(x); console.log(x.classList); 可以发现 获取了全部的class里的值 获取单 ...

  6. 使用fiddler抓取jmeter发送的请求

    使用jmeter发送请求时,有时需要查看发送的请求是否合理,可以使用fiddler更直观的抓取并查看jmeter发送的请求.步骤如下:1.设置fidder-connections 端口号为8888 2 ...

  7. C Vus the Cossack and Strings ( 异或 思维)

    题意 : 给你两个只包含 0 和 1 的字符串 a, b,定义函数 f ( A, B ) 为 字符串A和字符串B 比较 存在多少个位置 i 使得 A[ i ] != B[ i ] ,例如 f(0011 ...

  8. NOIP 模拟赛 那些年,我们学过的文化课 --致已退役的fqk神犇.

    /* 这大概是我第一次整理模拟赛吧. 唉. T2打了很长时间. 一开始读错题了中间都能缩合了. 真心对不起生物老师hhh. 这种状态判重的题目还是做的太少! */ 背单词 [题目描述] fqk 退役后 ...

  9. 7.6 T1 深度优先搜索(dfs)

    深度优先搜索(dfs) [题目描述] sol:50pts随便写写,就是大众分了,直接n2dpOK,100分要找点规律,需要数学头脑 官方题解 //#include <bits/stdc++.h& ...

  10. TNS-12560: TNS: 协议适配器错误

    TNS-12560: TNS: 协议适配器错误   Microsoft Windows [版本 5.2.3790] (C) 版权所有 1985-2003 Microsoft Corp. C:\Docu ...