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

目录
  • SpringMVC配置多个properties文件之通配符
    • xml文件中配置如下
    • 解决方案是:利用通配符
  • 多个SpringMVC项目配置统一管理
    • 1.首先是properties文件
    • 2.针对各类xml配置文件

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

(0)

相关推荐

  • spring+hibernate 两种整合方式配置文件的方法

    之前的文章都是讲解springmvc+spring+mybatis 的整合,而很少有springmvc+spring+hibernate 因为工作的需要,最近在使用hibernate 所以下面我们来看看 spring整合hibernate的配置文件,这里只说spring+hibernate 的配置文件而不说springmvc 因为这些是不用变的. spring整合hibernate 有两种方式 1.注解方式 2.xml方式实现 1.注解方式实现: applicationContext.xml配置

  • Spring中配置和读取多个Properties文件的方式方法

    一个系统中通常会存在如下一些以Properties形式存在的配置文件 1.数据库配置文件demo-db.properties: database.url=jdbc:mysql://localhost/smaple database.driver=com.mysql.jdbc.Driver database.user=root database.password=123 2.消息服务配置文件demo-mq.properties: #congfig of ActiveMQ mq.java.namin

  • spring如何加载配置多个配置文件

    这篇文章主要介绍了spring如何加载配置多个配置文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 为应用指定多个配置文件: 多个配置文件的关系: 并列 包含 并列关系 即有多个配置文件,需要同时加载这多个配置文件: 可以使用可变参数,数组和统配符进行加载: 可变参数 String config1 = "com/abc/di08/spring-student.xml"; String config2 = "com/abc/

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

    目录 SpringMVC配置多个properties文件之通配符 xml文件中配置如下 解决方案是:利用通配符 多个SpringMVC项目配置统一管理 1.首先是properties文件 2.针对各类xml配置文件 SpringMVC配置多个properties文件之通配符 在springmvc中配置加载properties文件一般会在 xml文件中配置如下 <context:property-placeholder location="classpath:resources/proper

  • springboot代码,注解配置获取yml,properties文件的map即键值对

    目录 注解配置获取yml,properties文件map即键值对 yml获取自定义键值对 properties 获取自定义键值对 properties配置应用,为什么需要使用properties文件 注解配置获取yml,properties文件map即键值对 yml获取自定义键值对 yml中的键值对 test:   map:     key1: value1     key2: value2     key3: value3 pom中的依赖配置 <dependency>     <gro

  • SpringBoot读取properties文件配置项过程解析

    使用SpringBoot开发过程中,难免需要配置相关数据项,然后在Java代码中@Autowired注入并使用. 我们应该如何读取properties文件中的配置项呢? 基于SpringBoot项目,配置项一般都存放在application.properties文件中.有2种常用的方法: 1.使用@Value注解标注在Field上面 2.使用@ConfigurationProperties注解标注在类或者方法上 为了讲解方便,附上application.properties文件配置好的数据项 如

  • 基于redis.properties文件的配置及说明介绍

    在使用到redis连接池时,需要进行一些redis相关配置,redis.properties文件是由编程者自己在项目classpath路径(如eclipse的src)下建立的,并非从redis安装包中获取的. 1.redis.properties文件的建立 在eclipse中找到相应的项目,选择File-->New-->File,选中项目中的src目录,填入文件名称redis.properties,然后Finish就可以了. 2.redis.properties文件的配置与说明 redis.t

  • java 如何读取properties文件

    1.情景展示 将要访问的接口地址等常用的配置添加到properties文件中,比直接写到java类中的好处在于: 当我们需要修改相应配置时,直接修改properties文件,重启tomcat即可,避免了重新编译引用该配置的java文件,同时,也便于项目的维护. 方式一 通过spring的工具类PropertiesLoaderUtils来实现对properties文件的解析 所需jar包:spring的核心jar包,spring-core-版本号.jar import java.io.IOExce

  • springMVC+jersey实现跨服务器文件上传

    本文实例为大家分享了springMVC+jersey实现跨服务器文件上传的具体代码,供大家参考,具体内容如下 1.首先添加所需要的jar包 2.在springMVC的配置文件中添加文件上传解析器 <!-- 文件上传的解析器 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> &l

  • 详解SpringMVC加载配置Properties文件的几种方式

    最近开发的项目使用了SpringMVC的框架,用下来感觉SpringMVC的代码实现的非常优雅,功能也非常强大, 网上介绍Controller参数绑定.URL映射的文章都很多了,写这篇博客主要总结一下SpringMVC加载配置Properties文件的几种方式 1.通过context:property-placeholde实现配置文件加载 1.1.在spring.xml中加入context相关引用 <?xml version="1.0" encoding="UTF-8&

  • 如何通过properties文件配置web.xml中的参数

    目录 前言 实现思路 web.xml中需要修改的部分 filter.properties文件 PropUtils工具类 查看web.xml参数 启动服务器进行测试 web.xml 前言 因为公司项目需要,目前有本地环境.测试环境.开发环境.每次在将项目打包成war包的时候,都需要修改多处的配置,而使用maven的profile打包项目的时候,可以根据执行打包命令时所带的参数来进行自动修改. 但是这种方式只对properties文件生效,即可以自动修改properties中的参数,但是公司的项目有

  • 将properties文件的配置设置为整个Web应用的全局变量实现方法

    四大作用域: Web应用中的变量存放在不同的jsp对象中,会有不一样的作用域,四种不同的作用域排序是 pageContext < request < session < application; 1.pageContext:页面域,仅当前页面有效,离开页面后,不论重定向还是转向(即无论是redirect还是forward),pageContext的属性值都失效: 2.request:请求域,在一次请求中有效,如果用forward转向,则下一次请求还可以保留上一次request中的属性值,

随机推荐