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" ...
随机推荐
- 计时器setTimeout()
setTimeout()计时器,在载入后延迟指定时间后,去执行一次表达式,仅执行一次. 语法: setTimeout(代码,延迟时间); 参数说明: 1. 要调用的函数或要执行的代码串. 2. 延时时 ...
- 文件上传(asp.net webform中)
1.配置允许上传文件大小 <configuration> <appSettings> <!--配置上传文件最大字节数:单位KB--> <add key=&qu ...
- Linux mysql 5.7.23 主从复制(异步复制)
docker容器主节点: 172.17.0.9 docker容器子节点: 172.17.0.10 异步复制: 首先确认主库和从库是否一致,最好都是刚刚初始化的干净的数据库 如果主库正在使用不能初始化 ...
- Java开发各层对象专用名词含义 PO,VO,DAO,BO,DTO,POJO, BYO,Entity,JavaBean,JavaBeans
Java的几种名词(PO,VO,DAO,BO,POJO)解释 PO:persistant object 持久对象.可以看成是与数据库中的表相映射的java对象.最简单的PO就是对应数据库中某个表中的一 ...
- CentOS设置网络,设置IP地址
1.登录CentOS. 2.# dhclient 自动获取IP地址 3.# ip addr 1).第一个地址127.0.0.1是回环地址,网卡名叫做lo,Windows操作系统也有该地址,用来和自己通 ...
- centos找不到vim命令
linux系统执行vim命令时,提示centos -bash : vim:command not found 这个时候需要检查vim编辑器是否安装: 输入rpm -qa|grep vim命令, ...
- 面向对象之—property,staticmethod
一 特性( property) property是一种特殊的属性,访问它时会执行一段功能(函数)然后返回值. property是内置的一种封装方法:把一个属性“伪装”成一个数据属性,做法就是在需要伪装 ...
- 由js文件中引入另外的js文件想到的
1. html中,使用<script src="../static/js/xxx.js"></script>引入js文件. 2. 在js文件中,引入js ...
- 零基础学习python_pickle(31课)
上次我提到了对文件的读写等一系列操作,回想下,要想从文件内读取内容无论是read还是readline,读取出来的是不是都是字符串呢?那么如果想让字典.列表这些数据类型保存进文件到读取出来都是原来的类型 ...
- 【Linux】【Maven】Linux下安装和配置Maven
创建maven的文件夹并下载maven的tar包到此文件夹中 //进入一个目录 cd /usr/local//创建一个文件夹 mkdir maven//下载maven的tar包 wget http:/ ...