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

在资源文件夹下面建立好要测试需要的app.propertise文件,里面写几条测试数据,本文主要如图数据。
二、准备配置文件
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.lilin"></context:component-scan> <!-- spring的属性加载器,加载properties文件中的属性 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:app.properties</value>
</property>
<property name="fileEncoding" value="utf-8" />
</bean> </beans>
配置文件中主要包含了两部分,都是用于测试的,一个是注解扫描的路径定义,让后面的测试类,在当前的扫描路径下,可以让spring管理be;一个是propertise文件的加载配置PropertyPlaceholderConfigurer,配置文件的读取路径和编码方式。
三、准备工具类
package com.lilin.maven.service.dynamicBean; import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.stereotype.Service;
import org.springframework.util.StringValueResolver; @Service
public class PropertiesUtils implements EmbeddedValueResolverAware { private StringValueResolver stringValueResolver; @Override
public void setEmbeddedValueResolver(StringValueResolver resolver) {
stringValueResolver = resolver;
} public String getPropertiesValue(String name) {
name = "${" + name + "}";
return stringValueResolver.resolveStringValue(name);
}
}
工具类实现EmbeddedValueResolverAware接口,实现必须的setEmbeddedValueResolver方法,把方法的resolver参数,赋值给当前工具类的私有属性,同时暴露出对外的提取函数getPropertiesValue,通过名称获取当前的对应的值。特别注意的是,resolveStringValue函数接受的name参数,一定是转换成“${name}”的方式才行,否则是取不到值的。
四、准备测试类
/**
*
*/
package com.lilin.maven.service.dynamicBean; import javax.annotation.Resource; import org.springframework.test.context.ContextConfiguration;
import org.testng.annotations.Test; import com.lilin.maven.service.BaseTest; /**
* @author lilin
*
*/
@ContextConfiguration(locations = { "classpath:/config/spring/spring-test.xml" })
public class DynamicBeanTest extends BaseTest { @Resource
private PropertiesUtils propertiesUtils; @Test
public void test() {
String beanName = propertiesUtils.getPropertiesValue("a");
System.out.println(beanName);
String beanName1 = propertiesUtils.getPropertiesValue("b");
System.out.println(beanName1);
}
}
测试类,主要使用testNG的测试方式,testNG的使用在前面的博文中已经有所介绍,请自行参阅。测试类中,把工具类注入PropertiesUtils,通过工具类,传入需要获取的参数名,获取对应的参数值。
Spring 获取propertise文件中的值的更多相关文章
- Spring获取properties文件中的属性
1.前言 本文主要是对这两篇blog的整理,感谢作者的分享 Spring使用程序方式读取properties文件 Spring通过@Value注解注入属性的几种方式 2.配置文件 applicatio ...
- @value 注解获取属性文件中的值
一.属性文件 db.properties name=jack 二.配置文件 applicationContext.xml <!-- 加载配置文件,该节点只能存在一个,所以用 * ,加载所有属性文 ...
- 封装的方法--读取任何路径下的properties文件中的值
概述:我们在做项目时,经常需要从某个properties文件中读取properties文件中的值.现在我封装了一下方法,直接读取配置文件中的值. 代码如下所示: /** * Created by qi ...
- XML序列化 判断是否是手机 字符操作普通帮助类 验证数据帮助类 IO帮助类 c# Lambda操作类封装 C# -- 使用反射(Reflect)获取dll文件中的类型并调用方法 C# -- 文件的压缩与解压(GZipStream)
XML序列化 #region 序列化 /// <summary> /// XML序列化 /// </summary> /// <param name="ob ...
- Android 资源文件local.properties使用以及Gradle文件中的值、Manifests文件中的值
这篇也是因为Gradle存储密钥问题一路填坑总结的,期初连.properties创建都有疑问 因为当时是在Android下查看新建的properties一直没法看到 因为Gradle Scripts是 ...
- 使用JavaScript设置、获取父子页面中的值
一:获取父页面中的值 有二种方法windows.open()和windows.showModalDialog() 1.windos.open(URL,name,reatures,replace) 再父 ...
- C#可以获取Excel文件中Sheet的名字
C#可以获取Excel文件中Sheet的名字吗 C#可以获取Excel文件中Sheet的名字吗 我试过WPS的表格可以 可以 要代码么 百度都有 [深圳]Milen(99696619) 14:13: ...
- 网站开发进阶(十八)js获取html标签中的值
js获取html标签中的值 项目开发过程中,由于需求所迫,需要获取html标签元素中的内容,下面做一简单总结.以下所讲的示例适用于其它标签元素. 主要包括2中方法获取元素内容: 方法一:.innerT ...
- Mybatis映射文件中#取值时指定参数相关规则
Mybatis映射文件中#取值时指定参数相关规则 在#{}中,除了需要的数值外,还可以规定参数的一些其他规则. 例如:javaType,jdbcType,mode(存储过程),numericScale ...
随机推荐
- PAT_A1142#Maximal Clique
Source: PAT A1142 Maximal Clique (25 分) Description: A clique is a subset of vertices of an undirect ...
- PAT_A1098#Insertion or Heap Sort
Source: PAT_A1098 Insertion or Heap Sort (25 分) Description: According to Wikipedia: Insertion sort ...
- eas之日志文件夹
F:\ThisIs_MyWork\kingdee\eas\server\profiles\server1\logs 服务端的日志文件夹 F:\ThisIs_MyWork\kingdeecusto ...
- 关于使用element中的popup问题
高产似母猪..写完上篇看了几集新番就空虚了..零点时分决定爬起来,趁着清明假期能写多写点. 1.前言 我们知道弹出框都是在触发了某种条件后展示,而一个个的新的弹出框的展示,总是覆盖着上一个弹出框.实现 ...
- js实现cookie有效期至当次日凌晨
实际开发中有要求用户一些行为每天一次,次日开始重新回复功能,一般前端都是通过cookie来记住用户的操作,然后进行判断当日是否还有机会,这时候需要给存储的cookie值一个有效期,让次日自动失效,重新 ...
- Summary of Memory Management Methods
Summary of Memory Management Methods Table 18-1 summarizes the various memory management methods. If ...
- 转载 - JTable 使用细讲
原文地址:http://hi.baidu.com/jiajiajava/item/1a18431b322fc011e2f986ef JTable是Swing编程中很常用的控件,这里总结了一些常用方法以 ...
- nutz_web应用中主页跳转到登录页面的方式
一.前言 web应用开发时,地址栏输入ip+port+appName,通常可以跳转到登录页面.以下便介绍我所知道并且验证过的三种跳转方式. 二.准备工作 需要使用到两个url的处理分别如下: @At( ...
- Leading and Trailing
You are given two integers: n and k, your task is to find the most significant three digits, and lea ...
- [bzoj2588][Spoj10628]Count on a tree_主席树
Count on a tree bzoj-2588 Spoj-10628 题目大意:给定一棵n个点的树,m次查询.查询路径上k小值. 注释:$1\le n,m\le 10^5$. 想法:好像更博顺序有 ...