• springboot中静态属性/静态方法从YAML(yml)读取配置属性


    启动类添加注解@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());
    ...
  • 相关阅读:
    Maven创建项目: Failed to execute goal org.apache.maven.plugin( mvn archetype:create)
    maven仓库--私服(Nexus的配置使用)
    maven仓库--私服(Nexus的配置使用)
    maven仓库--私服(Nexus的配置使用)
    maven中snapshot快照库和release发布库的区别和作用
    maven中snapshot快照库和release发布库的区别和作用
    maven中snapshot快照库和release发布库的区别和作用
    Bugzilla使用手册及解决方案
    jQuery常见的几个文档处理方式
    正则表达式实现对多个表格的某一指定字段的样式添加
  • 原文地址:https://www.cnblogs.com/passedbylove/p/11207827.html
Copyright © 2020-2023  润新知