启动类添加注解@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. 【转载】GAN for NLP 论文笔记

    本篇随笔为转载,原贴地址,知乎:GAN for NLP(论文笔记及解读).

  2. Python中的操作符及优先级

    附注: 1.  Python中的按位运算符是把数字看作二进制来进行计算的.Python中的按位运算法则如下: 按位与   ( bitwise and of x and y ) &  举例: 5 ...

  3. PAT乙级1031

    题目链接 https://pintia.cn/problem-sets/994805260223102976/problems/994805290334011392 题解 emmm.对于每个身份证号, ...

  4. shell小命令

    小括号的用途 cd ..; ls -l (cd ..; ls -l) 如果加了括号,则当前工作目录不发生改变 shell变量类型 环境变量 可以使用 echo  或者env 例如 env|grep 变 ...

  5. Redis——解决“org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'redisReferenceResolver': Unsatisfied dependency expressed through constructor parameter 0”

    错误栈: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ...

  6. 炫酷CSS3垂直时间轴特效

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. Python XML 解析Ⅱ

    make_parser方法 以下方法创建一个新的解析器对象并返回. 参数说明: parser_list - 可选参数,解析器列表 parser方法 以下方法创建一个 SAX 解析器并解析xml文档: ...

  8. 如何利用fiddler4 抓取手机的数据包

    1.安装fiddler . 2.设置fiddler  .tool==> option里面  https 要打开,然后选择actions 第一个 安装本地证书: 3.设置手机访问的数据都要经过fi ...

  9. 第十一天 unittest参数化模块

    unittest的工作原理:一下例子简单的描述了整个过程 def calc(a,b): return a//b import unittest,HTMLTestRunner import Beauti ...

  10. vue 剪切图片上传头像,使用 cropperjs 实现

    我使用的是vue,移动端的项目. 官网地址:cropperjs GitHub地址:https://github.com/fengyuanchen/cropperjs/blob/master/READM ...