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 了

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. redis 写磁盘出错 Can’t save in background: fork: Cannot allocate memory (转)

    查看 Redis 日志 发现系统在频繁报错: [26641] 18 Dec 04:02:14 * 1 changes in 900 seconds. Saving… [26641] 18 Dec 04 ...

  2. Android深入浅出之Binder机制(转)

    Android深入浅出之Binder机制 一 说明 Android系统最常见也是初学者最难搞明白的就是Binder了,很多很多的Service就是通过Binder机制来和客户端通讯交互的.所以搞明白B ...

  3. Android 开源库获取途径整理

    介绍眼下收藏 Android 开源库比較多的 GitHub 项目.站点.Twitter.App 及怎样获取最新的 Android 开源库. 微信号: 1. GitHub Android 开源项目汇总 ...

  4. 关于STM32 SPI NSS的讨论

    NSS分为内部引脚和外部引脚. NSS外部引脚可以作为输入信号或者输出信号, 输入信号一般用作硬件方式从机的片选, 而输出信号一般用于主SPI去片选与之相连的从SPI. NSS从设备选择有两种模式: ...

  5. STM32F4 -- How to use the DMA burst feature

    Bits 15:13 Reserved, must be kept at reset value. Bits 12:8 DBL[4:0]: DMA burst length This 5-bit ve ...

  6. MSDN WinUSB Example

    The WinUSB user-mode library uses device interface classes to communicate with the kernel-mode USB s ...

  7. STM32的CRC32 软件实现代码

    对于STM32的32位CRC,如果假定它的一个主要目的是为了校验往内部FLASH存储数据的可靠性,那么(余数)初值是全1当然是比较合理的.由于STM32的32位CRC是纯32位,即每次必须输入32位的 ...

  8. hdu Encoding

    Encoding Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Subm ...

  9. xamarin 断点 不命中

    Async Debugging Breakpoints not being hit breakpoint in Android library project not hit when disable ...

  10. iOS 项目一直在后台执行

    我后来是这么解决不知道行不行,能够长期的在后台执行 首先我在xx-info.plist 里的 "Required background modes" 里增加"App pr ...