使用这个工厂的配置,可以很方便的获取配置文件中的属性。具体使用如下;

对于属性配置,一般采用的是键值对的形式,如:
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的更多相关文章

  1. spring.factories配置文件的工厂模式

    在springboot的各个依赖包下,我们经常看到META-INF/spring.factories这个文件.spring.factories文件的内容基本上都是这样的格式: # Initialize ...

  2. 【转】spring管理属性配置文件properties——使用PropertiesFactoryBean|spring管理属性配置文件properties——使用PropertyPlaceholderConfigurer

     spring管理属性配置文件properties--使用PropertiesFactoryBean 对于属性配置,一般采用的是键值对的形式,如:key=value属性配置文件一般使用的是XXX.pr ...

  3. (精简)Spring框架的IoC(替代工厂类实现方法)和AOP(定义规则,约定大于配置)

    Spring的核心框架主要包含两个技术,分别用来处理工厂类,以及事务处理和连接管理的. 两大核心概念 1)  IoC:控制反转,在现在的开发中,如果想建立对象并设置属性,是需要先new对象,再通过se ...

  4. 四 Spring的工厂类,xml的配置

    Spring工厂类的结构图: BeanFactory:老版本的工厂类 BeanFactory:调用getBean的时候,才会生产类的实例 ApplicationFactory:新版本的工厂类 加载配置 ...

  5. Spring Boot集成Quartz注入Spring管理的类

    摘要: 在Spring Boot中使用Quartz时,在JOB中一般需要引用Spring管理的Bean,通过定义Job Factory实现自动注入. Spring有自己的Schedule定时任务,在S ...

  6. spring读取配置文件PropertyPlaceholderConfigurer类的使用

    这里主要介绍PropertyPlaceholderConfigurer这个类的使用,spring中的该类主要用来读取配置文件并将配置文件中的变量设置到上下文环境中,并进行赋值. 一.此处使用list标 ...

  7. spring 学习(二):spring bean 管理--配置文件和注解混合使用

    spring 学习(二)spring bean 管理--配置文件和注解混合使用 相似的,创建 maven 工程,配置pom.xml 文件,具体可以参考上一篇博文: sprint 学习(一) 然后我们在 ...

  8. spring 的工厂类

    spring 的工厂类 1. 工厂类 BeanFactory 和 ApplicationContext 的区别. ApplicationContext 是 BeanFactory 的子接口,提供了比父 ...

  9. 使用spring配置类代替xml配置文件注册bean类

    spring配置类,即在类上加@Configuration注解,使用这种配置类来注册bean,效果与xml文件是完全一样的,只是创建springIOC容器的方式不同: //通过xml文件创建sprin ...

随机推荐

  1. Linux常用命令快查

    一.读取配置文件中某一个变量的值 假如有一个配置文件dubbo.properties,需要读取dubbo.application.name的值: dubbo.application.name=book ...

  2. 2017年4月 TIOBE 编程语言排名

    2017年4月 TIOBE 编程语言排名 Hack是Facebook 在三年推出的PHP方言,在2017年4月首次进入TIOBE编程语言排行榜前50位. Hack原是Facebook的内部项目,与20 ...

  3. Git操作指南

    请访问以下网址,很详细,今天偷个懒记录一下,之后有时间再来补全吧! https://git-scm.com/book/zh/v2

  4. 机器学习笔记-1 Linear Regression with Multiple Variables(week 2)

    1. Multiple Features note:X0 is equal to 1 2. Feature Scaling Idea: make sure features are on a simi ...

  5. hadoop高可靠性HA集群

    概述 简单hdfs高可用架构图 在hadoop2.x中通常由两个NameNode组成,一个处于active状态,另一个处于standby状态.Active NameNode对外提供服务,而Standb ...

  6. poj3160强连通分量加dfs

    After retirement as contestant from WHU ACM Team, flymouse volunteered to do the odds and ends such ...

  7. linux 内核的futex pi-support,即pi-futex使用rt_mutex委托

    futex的pi-support,也就是为futex添加pi算法解决优先级逆转的能力,使用pi-support的futex又称为pi-futex.在linux内核的同步机制中,有一个pi算法的成例,就 ...

  8. Android计时器 android.widget.Chronometer

    说起做定时器,大家一般会想到Timer和Executors的定时器线程池,其实用这两个做都会有问题,在停止和重新计时时你回发现无法停止或者说计时加快(加快是因为多个线程在记录同一个变量),Androi ...

  9. javascript函数作用域及this指向详解

    一.先说一个简单的概念--变量提升: 通过function+函数名的方式,声明的函数,可以在代码中的任何位置调用: 通过var定义变量的方式,声明的函数,则必须在声明之后进行调用,原因就是在变量定义之 ...

  10. 使用java API操作hdfs--拷贝部分文件到本地

    要求:和前一篇的要求正好相反.. 在HDFS中生成一个130KB的文件: 代码如下: import java.io.IOException; import org.apache.hadoop.conf ...