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
猜你喜欢
- 1、概念向下转型就是父类对象转成子类对象。我们把一个父类引用 Animal类型的引用 给了一个 Bird类型 的引用,这就是向下转型2、格式
- springboot集成swagger3swagger3的springboot启动器jar包<!-- https://mvnrepos
- 这是一次阿里面试里被问到的题目,在我的印象中,final修饰的方法是不能被子类重写的。如果在子类中重写final修饰的方法,在编译阶段就会提
- 动态SQL简介动态 SQL 是 MyBatis 的强大特性之一。如果你使用过 JDBC 或其它类似的框架,你应该能理解根据不同条件拼接 SQ
- 摘要:在spring boot中 MVC这部分也有默认自动配置,也就是说我们不用做任何配置,那么也是OK的,这个配置类就是 WebMvcAu
- 前提最近发现各个频道推荐了很多ULID相关文章,这里对ULID的规范文件进行解读,并且基于Java语言自行实现ULID,通过此实现过程展示U
- 前言1、下面是一个效果展示;2、先抱怨一下,在博客上面的抄袭真的非常严重,为了实现一个图片滑动验证,我搜索了挺久的资料,不过内容翻来覆去就是
- 报错翻译: compileSdkVersion android-24”需要JDK 1.8或更高版本编译。报错现象如下图:原因:st
- 1. 单个属性yaml 内的属性如下:server: port: 80只需在成员变量上注解 @Value(“
- string fileExt = Path.GetExtension(excelPath);string conn = "&quo
- 在 javax.validation.constraints包中定义了非常多的校验注解,引入依赖:<dependency> &n
- 分析代码规范,并从代码角度分析存在的风险,并且支持一键更改。具体操作如下图:简单方便,最主要的是好用!补充:下面看下IntelliJ IDE
- 一.问题1.HashSet,TreeSet是如何使用hashCode()和equal()方法的2.TreeMap,TreeSet中的对象何时
- 现象说明maven的java项目,测试用例和main所在的源码文件均符合缺省写法和格式,但是在使用mvn clean sonar:sonar
- 平时我们在开发过程中,代码出现bug时为了更好的在服务器日志中寻找问题根源,会在接口的首尾打印日志,看下参数和返回值是否有问题。但是手动的l
- 一、项目简述本系统主要实现的功能有: 学生以及老师的注册登录,在线考试,错题查询,学生管理,问题管理,错题管理,错题查询,分数查询,试卷管
- 引言♀ 小AD:明哥,我终于出了这口恶气了。♂ 明世隐:打爽了是吧。♀ 小AD:那必须的,打十盘我赢九盘,我随意。♂ 明世隐:那小朋友不是搞
- 1、单点登录三种常见的方式(1)Session广播机制(Session复制)(2)使用Cookie+Redis实现(3)使用token实现2
- java 使用foreach遍历集合元素的实例1 代码示例import java.util.*; public class ForeachT
- Spring中BeanFactory FactoryBean和ObjectFactory的三种的区别引言关于FactoryBean 和 Be