spring管理配置文件的工厂类--PropertiesFactoryBean
使用这个工厂的配置,可以很方便的获取配置文件中的属性。具体使用如下;
对于属性配置,一般采用的是键值对的形式,如:
key=value
属性配置文件一般使用的是XXX.properties,当然有时候为了避免eclipse把properties文件转码,放到服务器上认不出中文,可以采用XXX.conf的形式管理属性配置。
spring对于属性文件有自己的管理方式,通过spring的管理,可以直接使用@Value的方式直接得到属性值。
先使用org.springframework.beans.factory.config.PropertiesFactoryBean对属性配置文件进行管理。
1.新建一个Javaproject,命名spring_test;
2.导入jar包:
aopalliance-1.0.jar
commons-logging-1.1..jar
org.springframework.test-3.1..RELEASE.jar
spring-aop-3.1..RELEASE.jar
spring-asm-3.1..RELEASE.jar
spring-beans-3.1..RELEASE.jar
spring-context-3.1..RELEASE.jar
spring-context-support-3.1..RELEASE.jar
spring-core-3.1..RELEASE.jar
spring-expression-3.1..RELEASE.jar
3、在resources下建立conf目录,并且conf目录下建立Application.properties
4、根据业务写相关配置:
state=
server.ip=10.10.33.174
server.port=
5、新建一个对象来获取配置属性
package com.atomview.signalgateway.config; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; /**
* Created on 2017/6/9.
*/ public class StreamServerProperties { @Value("#{propertiesReader['state']}")
private String m_ConnectState; @Value("#{propertiesReader['server.ip']}")
private String m_StreamserverIp; @Value("#{propertiesReader['server.port']}")
private String m_StreamserverPort; public String getConnectState() {
return m_ConnectState;
} public String getStreamserverIp() {
return m_StreamserverIp;
} public String getStreamserverPort() {
return m_StreamserverPort;
}
}
6、配置一下配置文件的工厂bean
<bean id="propertiesReader"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath*:/conf/application.properties</value>
<value>classpath*:/conf/streamserver.properties</value>
</list>
</property>
</bean> <bean id="serverproperties" class="com.atomview.signalgateway.config.StreamServerProperties"/>
7、测试类:
public static ApplicationContext getContext(){
ApplicationContext context = new FileSystemXmlApplicationContext("classpath:spring_framework.xml");
return context;
}
public static void main(String[] args){
ApplicationContext context = getContext();
StreamServerProperties tProperties = (StreamServerProperties) context.getBean("serverproperties");
System.out.println(tProperties.getStreamserverIp());
}
8、结果:
spring管理配置文件的工厂类--PropertiesFactoryBean的更多相关文章
- spring.factories配置文件的工厂模式
在springboot的各个依赖包下,我们经常看到META-INF/spring.factories这个文件.spring.factories文件的内容基本上都是这样的格式: # Initialize ...
- 【转】spring管理属性配置文件properties——使用PropertiesFactoryBean|spring管理属性配置文件properties——使用PropertyPlaceholderConfigurer
spring管理属性配置文件properties--使用PropertiesFactoryBean 对于属性配置,一般采用的是键值对的形式,如:key=value属性配置文件一般使用的是XXX.pr ...
- (精简)Spring框架的IoC(替代工厂类实现方法)和AOP(定义规则,约定大于配置)
Spring的核心框架主要包含两个技术,分别用来处理工厂类,以及事务处理和连接管理的. 两大核心概念 1) IoC:控制反转,在现在的开发中,如果想建立对象并设置属性,是需要先new对象,再通过se ...
- 四 Spring的工厂类,xml的配置
Spring工厂类的结构图: BeanFactory:老版本的工厂类 BeanFactory:调用getBean的时候,才会生产类的实例 ApplicationFactory:新版本的工厂类 加载配置 ...
- Spring Boot集成Quartz注入Spring管理的类
摘要: 在Spring Boot中使用Quartz时,在JOB中一般需要引用Spring管理的Bean,通过定义Job Factory实现自动注入. Spring有自己的Schedule定时任务,在S ...
- spring读取配置文件PropertyPlaceholderConfigurer类的使用
这里主要介绍PropertyPlaceholderConfigurer这个类的使用,spring中的该类主要用来读取配置文件并将配置文件中的变量设置到上下文环境中,并进行赋值. 一.此处使用list标 ...
- spring 学习(二):spring bean 管理--配置文件和注解混合使用
spring 学习(二)spring bean 管理--配置文件和注解混合使用 相似的,创建 maven 工程,配置pom.xml 文件,具体可以参考上一篇博文: sprint 学习(一) 然后我们在 ...
- spring 的工厂类
spring 的工厂类 1. 工厂类 BeanFactory 和 ApplicationContext 的区别. ApplicationContext 是 BeanFactory 的子接口,提供了比父 ...
- 使用spring配置类代替xml配置文件注册bean类
spring配置类,即在类上加@Configuration注解,使用这种配置类来注册bean,效果与xml文件是完全一样的,只是创建springIOC容器的方式不同: //通过xml文件创建sprin ...
随机推荐
- golang环境
Golang是谷歌开发的一款开源性语言,暂时比较方便的IDE有Inteillj Idea.LiteIDE.Eclipse(Golipse)等,使用起来比较方便的IDE:LiteIDE和Inteillj ...
- Linux 系统管理04--账号管理
Linux系统管理04--账号管理 一.用户账号管理 1.用户账号概述 (1)用户账号的常见分类: 1>超级用户:root uid=0 gid=0 权限最大. 2>普通用户:uid> ...
- 树莓派的GPIO编程
作者:Vamei 出处:http://www.cnblogs.com/vamei 严禁转载. 树莓派除了提供常见的网口和USB接口 ,还提供了一组GPIO(General Purpose Input/ ...
- phpcms 制作简单企业站的常用标签
标题 title 关键字 keywords 描述 description 来源 copyfrom 允许访问 allow_visitor==1 thumb 缩略图 {template "con ...
- 谈谈web上种图片应用的优缺点
web中承载信息的主要方式就是图片与文字了,以下就是对一些web图片格式的优缺点进行归纳. 1.GIF GIF图是比较古老的web图片格式之一,可以追溯到1987,几乎所有的浏览器都支持这一种格式,老 ...
- LeetCode 376. Wiggle Subsequence 摆动子序列
原题 A sequence of numbers is called a wiggle sequence if the differences between successive numbers s ...
- Azure IoT 技术研究系列5-Azure IoT Hub与Event Hub比较
上篇博文中,我们介绍了Azure IoT Hub的使用配额和缩放级别: Azure IoT 技术研究系列4-Azure IoT Hub的配额及缩放级别 本文中,我们比较一下Azure IoT Hub和 ...
- Collection的迭代器Iterator
Collection -- 迭代的方法 toArray() iterator() 迭代器的作用:抓取集合中的元素 迭代器的方法有 hasNext() next() remove() public ...
- day_1 练习2
#!/usr/bin/env python# -*- coding:utf-8 -*-'''b. 功能要求: v = 2000 要求用户输入总资产,例如:2000 显示商品列表,让用户根据序号选择商品 ...
- 使用gnuplot对tpcc-mysql压测结果生成图表
tpcc-mysql的安装:http://www.cnblogs.com/lizhi221/p/6814003.html tpcc-mysql的使用:http://www.cnblogs.com/li ...