posts - 570,  comments - 96,  views - 171万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

启动类添加注解@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());
...

 

posted on   你不知道的浪漫  阅读(13502)  评论(2编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
点击右上角即可分享
微信分享提示