SpringMVC配置多个properties文件之通配符解析

 更新时间:2021年09月08日 10:02:48   作者:朝着希望前进  
这篇文章主要介绍了SpringMVC配置多个properties文件之通配符解析,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

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,多思考,多总结,多实践,小改动,大用处。希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Springboot整合实现邮件发送的原理详解

    Springboot整合实现邮件发送的原理详解

    SpringBoot集成邮件服务非常简单,通过简单的学习即可快速掌握邮件业务类的核心逻辑和企业邮件的日常服务,本文给大家分享Springboot整合实现邮件发送的原理,一起看看吧
    2021-06-06
  • 详解SpringBoot与SpringCloud的版本对应详细版

    详解SpringBoot与SpringCloud的版本对应详细版

    这篇文章主要介绍了详解SpringBoot与SpringCloud的版本对应详细版,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09
  • 解决 java.lang.NoSuchMethodError的错误

    解决 java.lang.NoSuchMethodError的错误

    这篇文章主要介绍了解决 java.lang.NoSuchMethodError的错误的相关资料,需要的朋友可以参考下
    2017-06-06
  • spring依赖注入成功但在调用接口的时候拿到的依赖却是null问题

    spring依赖注入成功但在调用接口的时候拿到的依赖却是null问题

    这篇文章主要介绍了spring依赖注入成功但在调用接口的时候拿到的依赖却是null问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-12-12
  • SpringBoot之Json的序列化和反序列化问题

    SpringBoot之Json的序列化和反序列化问题

    这篇文章主要介绍了SpringBoot之Json的序列化和反序列化问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-06-06
  • 详细了解MVC+proxy

    详细了解MVC+proxy

    Java有两种代理方式,一种是静态代理,另一种是动态代理。对于静态代理,其实就是通过依赖注入,对对象进行封装,不让外部知道实现的细节。很多 API 就是通过这种形式来封装的
    2021-07-07
  • 解决RestTemplate反序列化嵌套对象的问题

    解决RestTemplate反序列化嵌套对象的问题

    这篇文章主要介绍了解决RestTemplate反序列化嵌套对象的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-11-11
  • 基于java实现websocket协议过程详解

    基于java实现websocket协议过程详解

    这篇文章主要介绍了基于java实现websocket协议过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-09-09
  • jsp+dao+bean+servlet(MVC模式)实现简单用户登录和注册页面

    jsp+dao+bean+servlet(MVC模式)实现简单用户登录和注册页面

    这篇文章主要介绍了jsp+dao+bean+servlet(MVC模式)实现简单用户登录和注册页面,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-12-12
  • MyBatis的collection和association的使用解读

    MyBatis的collection和association的使用解读

    这篇文章主要介绍了MyBatis的collection和association的使用解读
    2023-12-12

最新评论