在maven pom.xml中加载不同的properties ,如localhost 和 dev master等jdbc.properties 中的链接不一样

【参考】:maven pom.xml加载不同properties配置[转]

 首先 看看效果:

点开我们项目中的Maven projects 后,会发现右侧 我们profile有个可勾选选项。默认勾选localhost。localhost对应项目启动后,会加载配置左侧localhost文件夹下面的jdbc.perperties 和 redis.properties。 对于项目中存在测试数据库或者运行数据库的区分的时候 我们可以通过勾选快速切换。那么我们是如何实现的呢

一:在左侧文件目录项目 配置相对于的文件目录,并且注释掉你之前的加载 ,如图所示,

配置好所需的jdbc 链接   需要注意的 (正确写法)url=jdbc:mysql://127.0.0.1:3306/course_design?useUnicode=true&characterEncoding=UTF-8 中间的间隔符 必须用 & 替换掉之前写的 (错误写法)url=jdbc:mysql://127.0.0.1:3306/course_design?useUnicode=true&characterEncoding=UTF-8

具体原因参考   Caused by: org.xml.sax.SAXParseException; lineNumber: 30; columnNumber: 84; 对实体 "characterEncoding"

 

                                        

 

 二:配置 pom.xml

 

 <!--配置profiles-->
  <profiles>
    <profile>
      <id>localhost</id>
      <!--默认勾选设置-->
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
        <env>localhost</env>
      </properties>
    </profile>

    <profile>
      <id>dev</id>
      <properties>
        <env>dev</env>
      </properties>
    </profile>
    <profile>
      <id>master</id>
      <properties>
        <env>master</env>
      </properties>
    </profile>
  </profiles>

    <build>
        ......................................
    </build>

 

 

<build>
.............................
<!--指定待加载的文件位置--> <filters> <filter>configs/${env}/jdbc.properties</filter> <filter>configs/${env}/redis.properties</filter> </filters> <!--指定需要这些加载数据的 配置文件位置--> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/**</include> </includes> <filtering>true</filtering> </resource> </resources> </build>

 

理论上 这个时候 以及在pom,xml中 配置完成了 点击编译之后 如果 正常是可以的

但我这里报错了  3 字节的 UTF-8 序列的字节 3 无效   后来查询资料 发现还需要配置一个utf-8编译 【参考】http://blog.csdn.net/lzupb/article/details/53008481

<!-- resource插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>

 

 其实后来 还有一个问题 就是

  1. : Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener  
  2. org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 30 in XML document from class path resource [applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 30; columnNumber: 84; 对实体 "characterEncoding" 的引用必须以 ';' 分隔符结尾。  这样的的一个异常

这个时候 前面介绍的jdbc url写法就解决了  把&  变成 &amp; 就好了

 

posted @ 2017-03-30 14:35  草帽boy  阅读(2562)  评论(0编辑  收藏  举报