启动类添加注解@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. Docker容器内服务自启

    创建容器时需要配置--privileged和容器启动后执行的命令为/sbin/init/. docker run -d -it --name example -p 3308:3306 -p 2080: ...

  2. 差分约束详解&&洛谷SCOI2011糖果题解

    差分约束系统: 如果一个系统由n个变量和m个约束条件组成,形成m个形如ai-aj≤k的不等式(i,j∈[1,n],k为常数),则称其为差分约束系统(system of difference const ...

  3. 网络资源url转化为file对象下载文件

    注:只测试过网络图片资源. 一.使用org.apache.commons.io.FileUtils 二. 三.httpURLConnection.disconnect(); 四. import org ...

  4. CentOS5、CentOS6启动流程

    这三篇文章讲的都很好,可以看一下 http://os.51cto.com/art/201407/446819.htm http://www.mamicode.com/info-detail-11656 ...

  5. 【idea】idea 2018.2 for mac永久破解激活方法(亲测2099)

    1. 下载安装idea: 2. 下载激活Jar包 链接:https://pan.baidu.com/s/1NaxYrDNi2eW66epjmk10dg 密码:aec5 3. 在访达中新建/Librar ...

  6. Acwing-167-木棒(搜索, 剪枝)

    链接: https://www.acwing.com/problem/content/169/ 题意: 乔治拿来一组等长的木棒,将它们随机地砍断,使得每一节木棍的长度都不超过50个长度单位. 然后他又 ...

  7. 关于.Net中Process和ProcessStartInfor的使用

    本文主要是介绍在.Net中System.Diagnostics命名空间下Process类和ProcessStartInfo类的使用 用于启动一个外部程序所使用的类是Process,至于ProcessS ...

  8. gtid 同步1050异常处理

    gtid 同步1050异常处理 .sql CREATE TABLE `fudao_student_lable` ( `id` ) NOT NULL AUTO_INCREMENT, `uid` ) un ...

  9. [Python之路] 使用epoll实现高并发HTTP服务器

    什么是epoll 我们在  Python多种方式实现并发的Web Server 的最后使用单进程+单线程+非阻塞+长连接实现了一个可并发处理客户端连接的服务器.他的原理可以用以下的图来描述: 解释: ...

  10. Nowcoder的JS计时器题分析

    题目描述 实现一个打点计时器,要求1.从 start 到 end(包含 start 和 end),每隔 100 毫秒 console.log 一个数字,每次数字增幅为 12.返回的对象中需要包含一个 ...