• spring 读取配置文件,将值注入到静态字段


    resources/config/config-dev.properties

    es.ip.node=xxxxxxx
    cluster.name=xxxxxxx
    client.transport.sniff=xxxxxxx

    加载properties
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="ignoreResourceNotFound" value="true"/>
    <property name="locations">
    <list>
    <value>classpath:config/config-${env}.properties</value>
    </list>
    </property>
    </bean>

    对应属性的bean
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;

    /**
    * <p>Description:读取properties文件</p>
    *
    * @Author: Austin
    **/
    @Component
    public class ESParams {
    public static String esIpNode;
    public static String clusterName;
    public static String clientTransportSniff;

    @Value("${es.ip.node}")
    public void setEsIpNode(String esIpNode) {
    ESParams.esIpNode = esIpNode;
    }

    @Value("${cluster.name}")
    public void setClusterName(String clusterName) {
    ESParams.clusterName = clusterName;
    }

    @Value("${client.transport.sniff}")
    public void setClientTransportSniff(String clientTransportSniff) {
    ESParams.clientTransportSniff = clientTransportSniff;
    }
    }
    注意:标颜色的地方,稍有不慎就少加或者写错,另外需要将自动生成setter的方法的修饰符static去掉,否则spring无法注入

    现在可以在任何类中直接使用 ESParams.xxx 即可方便引用,如 ESParams.esIpNode 了


  • 相关阅读:
    ADO中的多层次数据集,类似于dataset
    工作流的设计
    Socket bind系统调用简要分析
    linux Network Address Translation NAT 转载 还需要整理
    生活20190602
    磁盘空间满的问题
    linux netfilter nat 实现 转载
    Socket 套接字的系统调用
    linux 网络编程 基础
    学习linux,不要找别人了,我有东西要发
  • 原文地址:https://www.cnblogs.com/austinspark-jessylu/p/8962915.html
Copyright © 2020-2023  润新知