启动类添加注解@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()); ...
本博客文章绝大多数为原创,少量为转载,代码经过测试验证,如果有疑问直接留言或者私信我。
创作文章不容易,转载文章必须注明文章出处;如果这篇文章对您有帮助,点击右侧打赏,支持一下吧。
创作文章不容易,转载文章必须注明文章出处;如果这篇文章对您有帮助,点击右侧打赏,支持一下吧。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 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编程运行原理