项目中需要获取一些万年不变的参数,比如单点登录的域名

怎么从多个文件配置中获取呢,原来spring早已经提供了类PropertyPlaceholderConfigurer

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.2.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<!-- 开启IOC注解扫描 -->
<context:component-scan base-package="org.great" /> <!-- 开启MVC注解扫描 -->
<mvc:annotation-driven /> <bean id="systemProperties" class="org.great.Untiltools.SystemProperties">
<property name="locations">
<list>
<value>classpath:/PropertiesTest/core.properties</value>
</list>
</property>
<property name="fileEncoding" value="utf-8"/>
</bean> </beans>
@Data
public class SystemProperties extends PropertyPlaceholderConfigurer {//继承这个类即可 private Properties mergedProp = new Properties();
@Override
protected Properties mergeProperties() throws IOException {//一定要重写,否则默认使用默认的
mergedProp = super.mergeProperties();
return mergedProp;
} public String getContextValue(String key) {
Assert.notNull(key, "key值无效");
if(mergedProp.containsKey(key)){
return (String) mergedProp.get(key);
}else {
throw new RuntimeException("key值不存在");
}
} 测试一下
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/springPropertise.xml")
public class SystemPropertiesTest { @Resource(name="systemProperties")
SystemProperties systemProperties;
@Test
public void test(){
String ss= systemProperties.getContextValue("database.maxWait");
int c=0; }


参考博客:http://blog.csdn.net/irokay/article/details/73010676

spring获取配制文件的参数的更多相关文章

  1. Spring 获取propertise文件中的值

    Spring 获取propertise文件中的值 Spring 获取propertise的方式,除了之前的博文提到的使用@value的注解注入之外,还可以通过编码的方式获取,这里主要说的是要使用Emb ...

  2. Spring获取properties文件中的属性

    1.前言 本文主要是对这两篇blog的整理,感谢作者的分享 Spring使用程序方式读取properties文件 Spring通过@Value注解注入属性的几种方式 2.配置文件 applicatio ...

  3. Spring 获取资源文件路径

    import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; s ...

  4. 【Spring】获取资源文件+从File+从InputStream对象获取正文数据

    1.获取资源文件或者获取文本文件等,可以通过Spring的Resource的方式获取 2.仅有File对象即可获取正文数据 3.仅有InputStream即可获取正文数据 package com.sx ...

  5. 使用rewrite 让php 实现类似asp.net 的IHttpModule 进行带参数js文件的参数获取

    asp.net 的IHttpModule 接口具有很大的作用,我们可以使用实现的模块进行全局的控制,但是在学习php 的过程中也想实现类似的功能,查找php 的文档,自己没有找到, 但是我们大家应该知 ...

  6. Spring MVC如何获取请求中的参数

    目录 一.获取URL中路径参数 1.1 @PathVariable 注解 1.2 @PathParam 注解 二.获取请求参数: 2.1 GET请求 2.1.1 获取请求中的单个参数:@Request ...

  7. 【spring Boot】spring boot获取资源文件的三种方式【两种情况下】

    首先声明一点,springboot获取资源文件,需要看是 1>从spring boot默认的application.properties资源文件中获取 2>还是从自定义的资源文件中获取 带 ...

  8. spring 通过@Value 获取properties文件中设置了属性 ,与@Value # 和$的区别

    spring 获取 properties的值方法 在spring.xml中配置 很奇怪的是,在context-param 加载的spring.xml 不能使用 ${xxx} 必须交给Dispatche ...

  9. phpStudy1——PHP文件获取html提交的参数

    示例代码: submit.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...

随机推荐

  1. WordPress更换主题空白问题

    刚才尝试着更换了一个主题,后来发现预览主页的时候是一片空白.查了很多资料,有说是index.php的权限问题,有说是插件问题,有说是UTL-8编码的问题,我都试过了,发现都不行,后来仔细研究了一下,发 ...

  2. jQuery中的跨域问题

    jQuery中的Ajax的跨域问题 jsonp跨域问题:回调函数中的字符串: cb或callback jsonpcallback:跟的是cb后面的函数,主要自己写: 数据中的函数fn(数据) 目前是这 ...

  3. Oracle 表空间的概念

    表空间   在数据库系统中,存储空间是较为重要的资源,合理利用空间,不但能节省空间,还可以提高系统的效率和工作性能. Oracle 可以存放海量数据,所有数据都在数据文件中存储.而数据文件大小受操作系 ...

  4. MySQL学习----explain查看一条sql 的性能

    在开发的过程中,对于我们写的sql语句,我们有时候会考虑sql语句的性能,那么explain就是首选.Explain命令在解决数据库性能上是第一推荐使用命令,大部分的性能问题可以通过此命令来简单的解决 ...

  5. 无法定位序数****于动态链接库LIBEAY32.dll上

    问题出现原因: GNS3打开出现问题 尝试的方法如下: 创建脚本: @echo 开始注册 copy libeay32.dll %windir%\system32 regsvr32 %windir%\s ...

  6. 在线学习和在线凸优化(online learning and online convex optimization)—基础介绍1

    开启一个在线学习和在线凸优化框架专题学习: 1.首先介绍在线学习的相关概念 在线学习是在一系列连续的回合(rounds)中进行的: 在回合,学习机(learner)被给一个question:(一个向量 ...

  7. eMTC/NB/LTE拨号

    挂起-恢复流程挂起恢复流程是eMTC/NB-IoT等蜂窝物联网技术才引进的,LTE并不具备这样的流程.这种机制的引入主要针对物联网海量连接,不活跃小数据包的特点,适时的挂起流程可以减少网络的资源开销, ...

  8. 由于ip改变重新配置CM集群

    修改所有主机/etc/hosts 修改所有agent节点的/opt/cm-5.5.1/etc/cloudera-scm-agent/config.ini,中server的ip 主节点启动cm serv ...

  9. Python利用脚本2.x到3自动转换

    本文介绍一下在windows 10 环境下如何使用这个工具: 1)首先要先安装好python3,可到官网下载https://www.python.org/ 2)使用Windows 命令提示符(cmd) ...

  10. Windows下sbt安装配置

    1.下载sbt1.2.8 官网:https://sbt-downloads.cdnedge.bluemix.net/releases/v1.2.8/sbt-1.2.8.tgz 2.配置环境 #SBT_ ...