spring获取配制文件的参数
项目中需要获取一些万年不变的参数,比如单点登录的域名
怎么从多个文件配置中获取呢,原来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获取配制文件的参数的更多相关文章
- Spring 获取propertise文件中的值
Spring 获取propertise文件中的值 Spring 获取propertise的方式,除了之前的博文提到的使用@value的注解注入之外,还可以通过编码的方式获取,这里主要说的是要使用Emb ...
- Spring获取properties文件中的属性
1.前言 本文主要是对这两篇blog的整理,感谢作者的分享 Spring使用程序方式读取properties文件 Spring通过@Value注解注入属性的几种方式 2.配置文件 applicatio ...
- Spring 获取资源文件路径
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; s ...
- 【Spring】获取资源文件+从File+从InputStream对象获取正文数据
1.获取资源文件或者获取文本文件等,可以通过Spring的Resource的方式获取 2.仅有File对象即可获取正文数据 3.仅有InputStream即可获取正文数据 package com.sx ...
- 使用rewrite 让php 实现类似asp.net 的IHttpModule 进行带参数js文件的参数获取
asp.net 的IHttpModule 接口具有很大的作用,我们可以使用实现的模块进行全局的控制,但是在学习php 的过程中也想实现类似的功能,查找php 的文档,自己没有找到, 但是我们大家应该知 ...
- Spring MVC如何获取请求中的参数
目录 一.获取URL中路径参数 1.1 @PathVariable 注解 1.2 @PathParam 注解 二.获取请求参数: 2.1 GET请求 2.1.1 获取请求中的单个参数:@Request ...
- 【spring Boot】spring boot获取资源文件的三种方式【两种情况下】
首先声明一点,springboot获取资源文件,需要看是 1>从spring boot默认的application.properties资源文件中获取 2>还是从自定义的资源文件中获取 带 ...
- spring 通过@Value 获取properties文件中设置了属性 ,与@Value # 和$的区别
spring 获取 properties的值方法 在spring.xml中配置 很奇怪的是,在context-param 加载的spring.xml 不能使用 ${xxx} 必须交给Dispatche ...
- phpStudy1——PHP文件获取html提交的参数
示例代码: submit.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...
随机推荐
- 由于代码已经过优化或者本机框架位于调用堆栈之上,无法计算表达式的值。System.Threading.ThreadAbortException
第一次遇到这样的错误 错误语法 try{ Response.Redirect("aa.aspx"); }catch (Exception ex){ Response.Redirec ...
- [UE4]扔枪
1.把枪Detach掉:DetachFromActor 3个都选择“Keep World” 2.模拟物理 3.给一个向前的速度 4.切枪,到上一个武器,或者捡起脚底下的武器 注意Get Compone ...
- 【Laravel】 常用的artisan命令【原创】
全局篇 查看artisan命令 php artisan php artisan list 查看某个帮助命令 php artisan help make:model 查看laravel版本 ...
- Redis 主从+哨兵+监控 (centos7.2 + redis 3.2.9 )
环境准备: 192.168.0.2 redis01 主 192.168.0.3 redis02 从 192.168.0.4 redis03 从 Redis 主从搭建 一:下载并安装redis软件 ...
- SAS数据集推送到sql server 数据库 实现代码段
libname fdsas ODBC datasrc=fdsas user=fdsas password=fdsas123 preserve_tab_names=yes connection=shar ...
- [电脑知识点]win10家庭版怎么显示桌面图标
控制面板-------->外观和个性化-------->个性化-------->更改桌面图标-------->然后你就能看到你想要的东西了,勾选上就可以了
- CSS便捷开发小工具汇总
1.Prefix free 可以帮助开发者省去编写各种CSS3属性前缀的工作,只需要在页面中引入prefixfree.js即可. 2. Normalize 是一个CSS Reset工具, 相比传统的R ...
- Java - 14 Java 日期时间
java.util包提供了Date类来封装当前的日期和时间. Date类提供两个构造函数来实例化Date对象. 第一个构造函数使用当前日期和时间来初始化对象. Date( ) 第二个构造函数接收一个参 ...
- Android手动控制软键盘的开启和关闭,判断软键盘是否显示;
工具类,拿走就能用: import android.annotation.TargetApi; import android.app.Activity; import android.content. ...
- WPF开发ArcGis系统时的异常信息: ArcGIS product not specified. You must first bind to an ArcGIS version prior to using any ArcGIS components.
“System.Runtime.InteropServices.COMException”类型的未经处理的异常在 Arcgis_Test.exe 中发生 其他信息: ArcGIS product no ...