SpringMVC配置多个properties文件之通配符解析
作者:朝着希望前进 发布时间:2021-10-18 02:19:02
SpringMVC配置多个properties文件之通配符
在springmvc中配置加载properties文件一般会在
xml文件中配置如下
<context:property-placeholder location="classpath:resources/properties/zza.properties"
ignore-unresolvable="true" />
如果希望在项目中添加了一个新的模块,并且希望新的模块和之前项目相对独立,需要新添加一个properties文件的话,那么需要在xml配置文件中,再配置一份。比如:
<context:property-placeholder location="classpath:resources/properties/zza.properties"
ignore-unresolvable="true" />
<context:property-placeholder location="classpath:resources/properties/weixin.properties"
ignore-unresolvable="true" />
这样做就太麻烦了,每次添加完properties文件还得在xml文件中添加。并且还必须把ignore-unresolvable属性设置为true。
解决方案是:利用通配符
具体如下:
<context:property-placeholder location="classpath*:resources/properties/*.properties" />
多个SpringMVC项目配置统一管理
来自于springCloud的统一配置思路
因公司项目分多个系统进行开发,而系统架构几乎完全一样,所以同样的配置文件会存在不同的系统中
当其中的某些配置需要修改时,就需要依次把所有系统中相关的配置都修改掉
纯耗时且没技术含量的体力活
所以借鉴SpringCloud的统一配置文件管理思想来对公司多个系统的配置文件也进行统一管理
1.首先是properties文件
针对诸如数据库连接等类似的共通信息,如果数据库信息发生变更则都需要修改,为了方便者直接在服务器上放置一个默认的连接配置
并发布到IIS等server上,通过http请求能够获取到
然后修改加载资源文件的配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>http://192.168.0.32:82/properties/jdbc.properties</value>
</list>
</property>
</bean>
</beans>
PropertyPlaceholderConfigurer默认是支持http和file方式加载资源的
2.针对各类xml配置文件
项目中除了web.xml外,还有众多的xml
和propertie文件一样,也是相同的配置文件存在于不同的项目中,一改就要挨个改,烦
同理,将xml发布,并修改IIS设置,使其通过浏览器能访问
iis需要增加MIME类型 properties和xml为text/plain才能在浏览器访问
然后就可以在浏览器访问了
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
http://192.168.0.32:82/springConfig/applicationContext-resource.xml,
http://192.168.0.32:82/springConfig/applicationContext-db.xml,
http://192.168.0.32:82/springConfig/applicationContext-redis.xml,
http://192.168.0.32:82/springConfig/applicationContext-redission.xml,
http://192.168.0.32:82/springConfig/applicationContext-service.xml,
http://192.168.0.32:82/springConfig/applicationContext-filter.xml
</param-value>
</context-param>
<servlet>
<description>spring-mvc</description>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
http://192.168.0.32:82/spring-mvc.xml
<!-- classpath:spring-mvc.xml -->
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
这样就可以直接启动了,启动时可以查看下面日志信息确定加载内容是正确的
最开始是修改为这样的
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
http://192.168.0.32:82/springConfig/applicationContext-*.xml
</param-value>
</context-param>
和classpath一样,但是很遗憾,解析不了统配费,找不到文件
java.io.FileNotFoundException: URL [http://192.168.0.32:82/springConfig/] cannot be resolved to absolute file path because it does not reside in the file system: http://192.168.0.32:82/springConfig/
at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:215)
at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:53)
at org.springframework.core.io.UrlResource.getFile(UrlResource.java:213)
at org.springframework.core.io.support.PathMatchingResourcePatternResolver.doFindPathMatchingFileResources(PathMatchingResourcePatternResolver.java:689)
at org.springframework.web.context.support.ServletContextResourcePatternResolver.doFindPathMatchingFileResources(ServletContextResourcePatternResolver.java:92)
at org.springframework.core.io.support.PathMatchingResourcePatternResolver.findPathMatchingResources(PathMatchingResourcePatternResolver.java:478)
at org.springframework.core.io.support.PathMatchingResourcePatternResolver.getResources(PathMatchingResourcePatternResolver.java:293)
仔细看源码 加载配置文件的源码 PathMatchingResourcePatternResolver中这段
@Override
public Resource[] getResources(String locationPattern) throws IOException {
Assert.notNull(locationPattern, "Location pattern must not be null");
if (locationPattern.startsWith(CLASSPATH_ALL_URL_PREFIX)) {
// a class path resource (multiple resources for same name possible)
if (getPathMatcher().isPattern(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length()))) {
// a class path resource pattern
return findPathMatchingResources(locationPattern);
}
else {
// all class path resources with the given name
return findAllClassPathResources(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length()));
}
}
else {
// Generally only look for a pattern after a prefix here,
// and on Tomcat only after the "*/" separator for its "war:" protocol.
int prefixEnd = (locationPattern.startsWith("war:") ? locationPattern.indexOf("*/") + 1 :
locationPattern.indexOf(":") + 1);
if (getPathMatcher().isPattern(locationPattern.substring(prefixEnd))) {
// a file pattern
return findPathMatchingResources(locationPattern);
}
else {
// a single resource with the given name
return new Resource[] {getResourceLoader().getResource(locationPattern)};
}
}
}
思路都很简单,配置的头尾解析出目录和含有通配符的文件,然后依次去找哪些文件满足
不过很遗憾的是,如果是http开头的通配符路径,暂时是不支持的,支持classpth,jar等方式
不过让人欣慰的是,是可以重写文件加载方式的,原因很简单,http目录知道了,要知道目录下面有哪些文件还是很简单的(需要开启iis的目录浏览),然后取到所有文件后,如果和通配符匹配,则加载
虽然有远端服务了,但是远端服务只是一个默认的全局配置,
为了方便本地修改部分参数进行调试,所以在需要的时候,修改部分xml地址为classpath中的,只是在提交代码的时候不要提交
若的确需要修改,则可以通知有服务器操作权限的人(我们公司比如我 ^_^)进行全局修改
以上仅为个人项目经验,其实就是把默认的classpath修改为了http,多思考,多总结,多实践,小改动,大用处。希望能给大家一个参考,也希望大家多多支持脚本之家。
来源:https://loong.blog.csdn.net/article/details/46777809


猜你喜欢
- 一、C#和JS互相调用 1、js调用C# C#代码如下: webView.CoreWebView2.AddHo
- 实现多表联合查询还是在david.mybatis.model包下面新建一个Website类,用来持久化数据之用,重写下相应toString(
- 本文实例为大家分享了java实现多个文件压缩的具体代码,供大家参考,具体内容如下需要用到的ant.jarpackage util;impor
- 写完布局后 我们一般需要 findViewById找到这个控件,但是现在有一个很好用的插件ButterKnife 可以一键转化布局文件中的所
- 背景在一些业务场景, 往往需要自定义异常来满足特定的业务, 主流用法是在catch里抛出异常, 例如:public void deal()
- 前言Kafka是现在非常热门的分布式消息队列,常用于微服务间异步通信,业务解耦等场景。kafka的性能非常强大,但是单个微服务吞吐性能是有上
- 装饰器模式概述装饰器模式(Decorator Pattern)也称为包装模式(Wrapper Pattern),属于结构型模式。它是指在不改
- Java for循环标签跳转到指定位置大家是否见过这种for循环,在for循环前加了个标记的:outerLoop:for (; ; ) {
- 关于 Android 实现 iOS 上的滚轮选择效果的控件,到 github 上一搜一大堆,之所以还要造这个轮子,目的是为了更好的学习自定义
- 和线程停止相关的三个方法/*中断线程。如果线程被wait(),join(),sleep()等方法阻塞,调用interrupt()会清除线程中
- 时间格式化在项目中使用频率是非常高的,当我们的 API 接口返回结果,需要对其中某一个 date 字段属性进行特殊的格式化处理,通常会用到
- 本文所述为一个由C#编写的音乐播放器的主Form代码,里面有一些小技巧还是不错的,现共享给大家参考一下。里面有播放器背景设置、线程定义、调用
- 本文实例为大家分享了Android自定义弹框Dialog效果的具体代码,供大家参考,具体内容如下1.dialog_delete.xml<
- 熟悉Eclipse的都知道Eclipse经常性的会出现一些莫名其妙的问题,有时候运行的好好的突然重启一下项目就莫名的报错,所以经常会用到cl
- 什么是二叉堆二叉堆就是完全二叉树,或者是靠近完全二叉树结构的二叉树。在二叉树建树时采取前序建树就是建立的完全二叉树。也就是二叉堆。所以二叉堆
- 在C/C++跨平台开发中,我们知道在Windows上可以通过VS,进行单步断点调试,这非常方便。但是我们如果编译好的动态库so,想要跟踪下其
- 一、概念:LINQ to Entities - ADO.NET | Microsoft 官方文档EF实体框架目前版本为EF6。EF6 可实现
- Okhttp 处理了很多网络疑难杂症,比如从很多常用的连接问题中自动恢复。如果你服务器配置了多个IP地址,当一个IP地址连接失败后Okhtt
- 1,现在因为遇到一个读取pdf文件文本信息遇到乱么问题,才找到这个文本字符串的编码转换的实现方式来判断是否存在乱码(0>乱码>2
- Java中List.of()和Arrays.asList()的区别及原因动手写一下,让自己更有印象1.Arrays.asList()可以插入