SpringMVC通过注解方式读取properties文件中的值
为什么80%的码农都做不了架构师?>>>
本方法是结合Java配置及XML配置来完成。
首先定义XML配置文件 app.xml:
<?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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
">
<util:properties id="app" location="classpath:spittr/props/app.properties"></util:properties>
<util:properties id="mess" location="classpath:spittr/i18n/messages_zh_CN.properties"></util:properties>
</beans>
然后在RootConfig.java配置文件中引入XML文件:
@Configuration
@ImportResource("classpath:spittr/**/app.xml")
@ComponentScan(basePackages={"spittr"},
excludeFilters={@Filter(type=FilterType.ANNOTATION,value=EnableWebMvc.class)})
public class RootConfig {}
RootConfig.java在用Java配置启动SpringMVC的时候用到:
public class SpittrWebAppInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {
//通常用来配置其它Bean,通常是驱动应用后端的数据层及中间层组件
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[]{RootConfig.class};
}
//通常用来配置控制,视图解析器以及处理器映射
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[]{WebConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
完成以上配置后,即可读取定义的properties文件中的值,
定义两个properties资源文件,spittr/props/app.properties,spittr/i18n/messages_zh_CN.properties,文件名前为路径,
app.properties内容如下:
spittr.welcome=欢迎来到Spittr世界!
messages_zh_CN.properties内容如下:
spittr=欢迎使用Spittr系统
测试代码:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=RootConfig.class)
public class I18nTest {
@Value("#{app['spittr.welcome']}")
private String wel;
@Value("#{mess['spittr']}")
private String spit;
@Test
public void test(){
System.out.println(wel);
System.out.println(spit);
}
}
@RunWith的作用是在测试的时候自动创建Spring的应用上下文,注解@ContextConfiguration告诉它需要在测试代码里加载指定的配置文件,即在测试代码中引入了相关的资源文件,因此能正常通过SpEL表达式读取properties文件中的值。
资料参考:《Spring实战(第4版)》
转载于:https://my.oschina.net/daigd/blog/758094
SpringMVC通过注解方式读取properties文件中的值相关推荐
- SpringBoot读取properties文件中的值
SpringBoot读取properties文件中的值 properties文件(test.properties) xx.xyz.url=http://www.xxx.com/ xx.xyz.name ...
- @Value取application.properties文件中的值
问题描述 在使用@Value取application.properties文件中的值时,取出的值为null 问题解决: 1. 需要在所在的类上加@Component,把普通pojo实例化到spring ...
- 使用spring最简单地读取properties文件中的内容
相比传统的读取propertis文件内容,使用spring框架会更加简单快捷 1. 第一步,在spring的配置文件中,将propertis文件加载到spring容器 2. 加载了配置文件后,只需要在 ...
- 超简单:解析 yml 类型(application.yml)配置文件 、springboot 工程读取 yml 文件中的值
方法三是我觉得最简单的. 前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. 1. 工程结构: 2. 我要读取 application.yml 中属性 ...
- android 读取sd卡中string.xml,android,从SD卡读取xml文件中的值
helo,我想从这个文件中读取值:http://www.nbp.pl/kursy/xml/lastC.xml并用它们填充网格.我的应用程序下载此文件并将其保存在SDCard earch运行时间.我的问 ...
- python读取CSV文件中温度值绘制3D折线图
import pyecharts.options as opts from pyecharts.charts import Line3D import random import csv filena ...
- dcmtk读取dcm文件中Tag值
读取病人信息等,可根据宏定义自由设置读取其他信息,代码如下: { DcmFileFormat fileformat;OFCondition dcmFile = fileformat.loadFile( ...
- java中读取properties文件内容五种方式
一.背景 最近,在项目开发的过程中,遇到需要在properties文件中定义一些自定义的变量,以供java程序动态的读取,修改变量,不再需要修改代码的问题.就借此机会把Spring+SpringMVC ...
- 五种方式让你在java中读取properties文件内容不再是难题
2019独角兽企业重金招聘Python工程师标准>>> 方式1.通过context:property-placeholder加载配置文件jdbc.properties中的内容 < ...
最新文章
- 几种经典的居中技巧(垂直和水平居中)
- SAP MM 同一个序列号可以被多次用在交货单发货过账?
- word rectangle
- 服务器端大文件如何传给客户端,客户端往服务器传文件
- c语言ifft,用于ARM上的FFT与IFFT源代码-C语言
- CORS 跨域-同源策略
- hades武器第四形态解锁_凯多的第四个技能预告——冰冻!
- 和硕看重物联网大势 程建中:从擅长领域出发
- SVM分类算法的基本理论问题
- 【Mybatis框架】输入映射-pojo包装类型
- es6 取数组的第一个和最后一个_全球最恐怖的3个泳池,第一个有10层楼高,最后一个听名字就害怕...
- 用汇编的眼光看C++(之模板类)
- Docker下安装GitLab
- [转]邵雍其诗、其人、其事
- Java 容器源码分析之 TreeMap
- MySQL数据库权限操作指南
- 摄影构图如何脱离规则套路
- 【元胞自动机】基于元胞自动机实现交通流NaSch模型matlab源码
- css椭圆轨迹运动动画
- 洛谷 P1725 琪露诺 题解