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

怎么从多个文件配置中获取呢,原来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. 基于Html的SEO(很基础,更是前端必须掌握之点)

    文章来源:百度文库   Html代码seo优化最佳布局,实例讲解     众所周知,搜索引擎对html代码是非常优化的,所以html的优化是做好推广的第一步.一个符合seo规则的代码大体如下界面所示. ...

  2. openstack处理booting from hard disk

    当你的实例启动出现如下报错的处理方式:         需要编辑/etc/nova/nova.conf [libvirt] cpu_mode = none virt_type=qemu   修改后需要 ...

  3. [UE4]The global shader cache file missing 运行错误解决办法

    UE4项目在VS中对项目代码编译时报如错,找了好久在UE4论坛上查到了别人的解决方案,贴出来仅供大家参考. 看到一位开发者解释出错的原因如下: There are a number of build ...

  4. [UE4]多播(广播)

    只有服务器才有权限做广播,所以要判断确保是服务器端才做广播,有以下几种方法: 一.使用“Switch Has Authority”判断是否在服务器端 因为character一定是在服务器端创建出来的, ...

  5. 总结一下连日来在MAC下被Python3设下的坑

    当时的情况:mac下自带python2, 1.安装pyhon3: 首次从官网下载了安装包安装,安装目录在/Library/Frameworks/Python.framework/Versions/3. ...

  6. http和https的区别联系

    超文本传输协议HTTP协议被用于在Web浏览器和网站服务器之间传递信息,HTTP协议以明文方式发送内容,不提供任何方式的数据加密,如果攻击者截取了Web浏览器和网站服务器之间的传输报文,就可以直接读懂 ...

  7. Python基础知识(五)

    # -*- coding: utf-8 -*-# @Time : 2018-12-25 19:31# @Author : 三斤春药# @Email : zhou_wanchun@qq.com# @Fi ...

  8. 第1章 计算机网络和协议(3)_TCP/IP协议

    3. TCP/IP协议 3.1 TCP/IP协议分层 3.2 TCP/IP通信过程 (1)应用层:浏览器和Web服务器是两个对等的实现,它们之间使用http协议进行通信. (2)传输层:网页传输之前, ...

  9. 插件开发-滑条(slide)开发

    自己一直很喜欢开发组件,只是OPP学的不是很精,自己在项目中用别人的框架进行项目开发,难免受制于人,也许这就是个人实际项目需求和框架提供的多少有点不符,引导我自己尝试开发一些自己常用的组件,话不多说, ...

  10. Python工程化小结

    对如何写一个工业级的Python项目作一个top-down小结. 一.项目结构 顶层结构: 文件夹: model可以是项目中的自定义类: utils是一些工程工具,比如log,tracker log存 ...