resources/config/config-dev.properties

  1. es.ip.node=xxxxxxx
    cluster.name=xxxxxxx
    client.transport.sniff=xxxxxxx
  2.  
  3. 加载properties
  1. <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>
  2.  
  3. 对应属性的bean
  1. import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
  2.  
  3. /**
    * <p>Description:读取properties文件</p>
    *
    * @Author: Austin
    **/
    @Component
    public class ESParams {
    public static String esIpNode;
    public static String clusterName;
    public static String clientTransportSniff;
  4.  
  5. @Value("${es.ip.node}")
    public void setEsIpNode(String esIpNode) {
    ESParams.esIpNode = esIpNode;
    }
  6.  
  7. @Value("${cluster.name}")
    public void setClusterName(String clusterName) {
    ESParams.clusterName = clusterName;
    }
  8.  
  9. @Value("${client.transport.sniff}")
    public void setClientTransportSniff(String clientTransportSniff) {
    ESParams.clientTransportSniff = clientTransportSniff;
    }
    }
  1. 注意:标颜色的地方,稍有不慎就少加或者写错,另外需要将自动生成setter的方法的修饰符static去掉,否则spring无法注入
  2.  
  3. 现在可以在任何类中直接使用 ESParams.xxx 即可方便引用,如 ESParams.esIpNode
  4.  

spring 读取配置文件,将值注入到静态字段的更多相关文章

  1. SpringBoot 读取配置文件的值 赋给静态变量

    需求:写了一个工具类,但是工具类中的一些变量需要放到配置文件中,而这个工具类中的变量与方法都是静态的,这个时候我需要一个办法将配置文件中的相关配置读取过来赋值给这些静态变量.找了一些文章,试了一些方法 ...

  2. SSH框架系列:Spring读取配置文件以及获取Spring注入的Bean

    分类: [java]2013-12-09 16:29 1020人阅读 评论(0) 收藏 举报 1.简介 在SSH框架下,假设我们将配置文件放在项目的src/datasource.properties路 ...

  3. java web路径和spring读取配置文件

    此篇博客缘起:部署java web系统到阿里云服务器(ubuntu14.04)的时候,有以下两个问题 找不到自定义的property配置文件 上传图片的时候找不到路径 开发的时候是在windows上的 ...

  4. Spring 读取配置文件(二)

    Spring 读取配置文件并调用 bean package cn.com.test.receive; import org.springframework.beans.factory.annotati ...

  5. Spring 读取配置文件(一)

    注册 @Configuration 标识的类,spring 读取配置文件的时候该类会被自动装载 package cn.com.receive;import org.springframework.be ...

  6. spring 获取配置文件的值

    Spring 获取配置文件的值 package com.hafiz.www.util; import org.slf4j.Logger; import org.slf4j.LoggerFactory; ...

  7. Spring MVC 属性文件读取注入到静态字段

    目录(?)[-] servlet-contextxml configproperties 示例属性 ConfigInfo 对应的配置bean 使用   在项目中,有些参数需要配置到属性文件xxx.pr ...

  8. 将springboot配置文件中的值注入到静态变量

    SpringBoot配置文件分为.properties和.yml两种格式,根据启动环境的不同获取不同环境的的值. spring中不支持直接注入静态变量值,利用spring的set注入方法注入静态变量 ...

  9. spring 运行时属性值注入

    继续spring学习,今天介绍两种外部属性值注入的方式.当你需要读取配置信息时,可以快速读取. 开始之前先创建属性文件site.properties,放在classpath下面 #数据库配置 ### ...

随机推荐

  1. CodeSmith 基础用法和例子

    〇.            前言 一.            工具设置 CodeSmith默认是不支持中文的,那么我们必须要先设置使其支持中文显示,保存.并且要能够在生成文件中支持中文. [Tools ...

  2. STM32 microcontroller system memory boot mode

    The bootloader is stored in the internal boot ROM memory (system memory) of STM32 devices. It is pro ...

  3. OFbiz--简单介绍

    一.简单介绍 OFBiz是一个很著名的电子商务平台,是一个很著名的开源项目,提供了创建基于最新J2EE/XML规范和技术标准,构建大中型企业级.跨平台.跨数据库.跨应用server的多层.分布式电子商 ...

  4. 发布网站时应该把debug设置false

    在ASP.NET项目根目录下的Web.config中有这样的一个节点: <compilation debug="true" targetFramework="4.5 ...

  5. 委托、Lambda表达式、事件系列07,使用EventHandler委托

    谈到事件注册,EventHandler是最常用的. EventHandler是一个委托,接收2个形参.sender是指事件的发起者,e代表事件参数. □ 使用EventHandler实现猜拳游戏 使用 ...

  6. npm WARN saveError ENOENT: no such file or directory

    转自树之名原文npm WARN saveError ENOENT: no such file or directory解决 我是在安装sequelize时出错的.提示的错误没有保存,类似于参考的文章中 ...

  7. 转:iOS 越狱的原理是什么

         本文的主角是一个苦逼的程序员,最近打算开始研究越狱破解,因为打算做类似一类越狱插件的东西给自己使用.所以就了解了一下越狱破解的一个原理过程. 每个手机都会有手机系统,苹果也不例外.每个手机系 ...

  8. SQL:两种获取时间类型日期部分的方法

    参考网址:http://www.w3school.com.cn/sql/sql_dates.asp. ), PassedDate, ), , PassedDate), )

  9. “finally block does not complete normally”的警告解决

    但是,java里面不是可以保证finally一定会执行的么,为什么不可以在finally块做return??? 细细看道来: debug一下这个函数,就会惊讶的发现, 里面抛出的异常会被finally ...

  10. 用jpinyin实现汉字转拼音功能

    一.简介 项目地址:https://github.com/stuxuhai/jpinyin JPinyin是一个汉字转拼音的Java开源类库,在PinYin4j的功能基础上做了一些改进. [JPiny ...