亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

使用spring+maven不同環(huán)境讀取配置方式

 更新時(shí)間:2023年08月29日 17:14:06   作者:小淼同學(xué)  
這篇文章主要介紹了使用spring+maven不同環(huán)境讀取配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

首先這個(gè)我看了網(wǎng)上很多資料,但我發(fā)現(xiàn),由于自己一些技術(shù)的不熟悉,對(duì)于他人的文章有些誤解,導(dǎo)致我打包部署失敗。

話不多說(shuō),現(xiàn)在我們開(kāi)始一步步工程

第一步,項(xiàng)目讀取不通環(huán)境的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
    <!-- 測(cè)試環(huán)境配置文件 -->
    <beans profile="local">
        <context:property-placeholder location="classpath:local/config.properties" />
    </beans>
    <!-- 生產(chǎn)環(huán)境配置文件 -->
    <beans profile="product">
        <context:property-placeholder location="classpath:product/config.properties" />
    </beans>
</beans>

這是我定義的一個(gè)讀配置文件的xml——applicationContext-profile.xml

這是由web.xml里面配置,來(lái)區(qū)分去讀哪個(gè)配置文件

 <!-- 配置spring的默認(rèn)profile
         可選值:product(生產(chǎn)環(huán)境) local(本地環(huán)境)  -->
  <context-param>
    <param-name>spring.profiles.active</param-name>
    <param-value>product</param-value>
  </context-param>
  <!-- spring hibernate -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
          classpath:public/spring/applicationContext-profile.xml
          classpath:public/spring/spring-hibernate.xml
    </param-value>
  </context-param>

web.xml的一部分,根據(jù)配置去讀product還是local,這里根據(jù)配置字段能看到對(duì)于的配置文件,這對(duì)應(yīng)關(guān)系應(yīng)該是能看懂的!

到這里,你應(yīng)該能根據(jù)自己的配置文件去運(yùn)行程序

第二步,根據(jù)pom文件打包

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>officialProject</artifactId>
        <groupId>com.wm</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>offical-web</artifactId>
    <packaging>war</packaging>
    <name>offical-web</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <shiro-version>1.3.2</shiro-version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>com.wm</groupId>
            <artifactId>offical-manager-service</artifactId>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>com.wm</groupId>
            <artifactId>official-manager-core</artifactId>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>com.wm</groupId>
            <artifactId>offical-common</artifactId>
            <type>jar</type>
        </dependency>
        <!-- mysql數(shù)據(jù)庫(kù)驅(qū)動(dòng) -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.37</version>
        </dependency>
        <!--后臺(tái)權(quán)限控制框架-->
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-core</artifactId>
            <version>${shiro-version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-spring</artifactId>
            <version>${shiro-version}</version>
        </dependency>
    </dependencies>
    <profiles>
        <profile>
            <id>local</id><!-- 本地開(kāi)發(fā)環(huán)境 -->
            <properties>
                <package.env>local</package.env>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>product</id><!-- 生產(chǎn)環(huán)境 -->
            <properties>
                <package.env>product</package.env>
            </properties>
        </profile>
    </profiles>
    <build>
        <finalName>${project.artifactId}</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <excludes>
                    <exclude>local/*</exclude>
                    <exclude>product/*</exclude>
                </excludes>
                <includes>
                    <include>public/**/*</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>com/wm/back/entity/**/hbm/*.hbm.xml</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <!-- jetty插件 -->
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.3.8.v20160314</version>
                <configuration>
                    <webAppConfig>
                        <contextPath>/</contextPath>
                        <defaultsDescriptor>src/main/resources/public/webdefault.xml</defaultsDescriptor>
                    </webAppConfig>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                    <archive>
                        <addMavenDescriptor>false</addMavenDescriptor>
                    </archive>
                    <warName>${project.artifactId}</warName>
                    <webResources>
                        <!-- 不同的環(huán)境,使用不同的配置文件 -->
                        <resource>
                            <directory>src/main/resources/${package.env}</directory>
                            <targetPath>WEB-INF/classes/${package.env}</targetPath>
                            <filtering>true</filtering>
                        </resource>
                        <!-- 公共的配置文件 -->
                        <resource>
                            <directory>src/main/resources/public</directory>
                            <!--targetPath用來(lái)指定文件放到哪里-->
                            <targetPath>WEB-INF/classes/public</targetPath>
                            <filtering>true</filtering>
                        </resource>
                        <resource>
                            <directory>src/main/webapp/WEB-INF/config</directory>
                            <targetPath>WEB-INF</targetPath>
                            <filtering>true</filtering>
                            <includes>
                                <include>web.xml</include>
                            </includes>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

先貼出全部代碼,然后再一一作出解釋

<profiles>
    <profile>
        <id>local</id><!-- 本地開(kāi)發(fā)環(huán)境 -->
        <properties>
            <package.env>local</package.env>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>product</id><!-- 生產(chǎn)環(huán)境 -->
        <properties>
            <package.env>product</package.env>
        </properties>
    </profile>
</profiles>

properties里配置的,下文都是可以使用的,我這里配置package.env  ,所以下面使用package.env就是代表local或者product根據(jù)我傳入的 -P后面的那個(gè)值來(lái)決定

打包語(yǔ)句是mvn clean package -P +(我傳入的值 profile的id)來(lái)決定package.env的變量

完整的打包語(yǔ)句是mvn clean package -P local

<activeByDefault>true</activeByDefault>

來(lái)決定激活哪個(gè)profile

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
        <excludes>
            <exclude>local/*</exclude>
            <exclude>product/*</exclude>
        </excludes>
        <includes>
            <include>public/**/*</include>
        </includes>
    </resource>
    <resource>
        <directory>src/main/java</directory>
        <includes>
            <include>com/wm/back/entity/**/hbm/*.hbm.xml</include>
        </includes>
    </resource>
</resources>

在我的理解下,不加resources的話,所有項(xiàng)目里的resources文件都會(huì)被加載,但java里面的配置文件可能還是加載不了的

但你定義了resources,你就要把你需要通過(guò)加載的文件都要寫出來(lái),不然他不會(huì)去加載。

我這里把各環(huán)境的配置文件都取消了加載,把通用的資源文件進(jìn)行了加載。

忘記提一句,這樣下,項(xiàng)目本地啟動(dòng)的時(shí)候會(huì)報(bào)錯(cuò),因?yàn)槟惆裭ocal或product都去掉了,程序找不到里面的文件。這只是打包的時(shí)候才試用

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.1.1</version>
    <configuration>
        <archive>
            <addMavenDescriptor>false</addMavenDescriptor>
        </archive>
        <warName>${project.artifactId}</warName>
        <webResources>
            <!-- 不同的環(huán)境,使用不同的配置文件 -->
            <resource>
                <directory>src/main/resources/${package.env}</directory>
                <targetPath>WEB-INF/classes/${package.env}</targetPath>
                <filtering>true</filtering>
            </resource>
            <!-- 公共的配置文件 -->
            <resource>
                <directory>src/main/resources/public</directory>
                <!--targetPath用來(lái)指定文件放到哪里-->
                <targetPath>WEB-INF/classes/public</targetPath>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/webapp/WEB-INF/config</directory>
                <targetPath>WEB-INF</targetPath>
                <filtering>true</filtering>
                <includes>
                    <include>web.xml</include>
                </includes>
            </resource>
        </webResources>
    </configuration>
</plugin>

打war包的時(shí)候需要這樣配置

  • directory:代表你要加載的目錄
  • targetPath:代表你要把你加載的文件放在哪里
<filtering>true</filtering>

這個(gè)網(wǎng)上說(shuō)一定要設(shè)置為true,在我看來(lái)沒(méi)什么關(guān)系,因?yàn)槲腋鶕?jù)命令打包,也能把文件打出來(lái)

 <resource>
                <directory>src/main/webapp/WEB-INF/config</directory>
                <targetPath>WEB-INF</targetPath>
                <filtering>true</filtering>
                <includes>
                    <include>web.xml</include>
                </includes>
            </resource>

我這里是為了把web.xml選擇讀哪個(gè)配置文件給替換掉

為什么要這樣設(shè)計(jì)能,因?yàn)槿绻贿@樣,我們本地啟動(dòng)項(xiàng)目,pom是不會(huì)吧${package.env}這個(gè)替換的。所以這樣會(huì)報(bào)錯(cuò)。

但我們打包的時(shí)候希望靈活替換。所以就寫一個(gè)config文件目錄,里面放過(guò)web.xml,當(dāng)要打包的時(shí)候替換WEB-INF下的web.xml文件就可以了

到此,一個(gè)完整的流程就走完了

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • springboot json時(shí)間格式化處理的方法

    springboot json時(shí)間格式化處理的方法

    這篇文章主要介紹了springboot json時(shí)間格式化處理的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-03-03
  • 基于IntBuffer類的基本用法(詳解)

    基于IntBuffer類的基本用法(詳解)

    下面小編就為大家?guī)?lái)一篇基于IntBuffer類的基本用法(詳解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-05-05
  • java常用工具類 Reflect反射工具類、String字符串工具類

    java常用工具類 Reflect反射工具類、String字符串工具類

    這篇文章主要為大家詳細(xì)介紹了java常用工具類,包括Reflect反射工具類、String字符串工具類,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • SpringBoot整合Dozer映射框架流程詳解

    SpringBoot整合Dozer映射框架流程詳解

    dozer是用來(lái)兩個(gè)對(duì)象之間屬性轉(zhuǎn)換的工具,有了這個(gè)工具之后,我們將一個(gè)對(duì)象的所有屬性值轉(zhuǎn)給另一個(gè)對(duì)象時(shí),就不需要再去寫重復(fù)的set和get方法了,下面介紹下SpringBoot中Dozer的使用,感興趣的朋友一起看看吧
    2022-07-07
  • Java SpringBoot整合JSP和MyBatis

    Java SpringBoot整合JSP和MyBatis

    這篇文章主要介紹了SpringBoot如何整合JSP和MyBatis以及SpringBoot的基本設(shè)置,感興趣的小伙伴可以參考閱讀
    2023-03-03
  • 淺談java中的訪問(wèn)修飾符

    淺談java中的訪問(wèn)修飾符

    這篇文章介紹了java中的訪問(wèn)修飾符,有需要的朋友可以參考一下
    2013-10-10
  • java啟動(dòng)如何設(shè)置JAR包內(nèi)存大小

    java啟動(dòng)如何設(shè)置JAR包內(nèi)存大小

    這篇文章主要介紹了java啟動(dòng)如何設(shè)置JAR包內(nèi)存大小問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • SpringBoot如何統(tǒng)一清理數(shù)據(jù)

    SpringBoot如何統(tǒng)一清理數(shù)據(jù)

    這篇文章主要介紹了SpringBoot如何統(tǒng)一清理數(shù)據(jù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • springboot?通過(guò)博途獲取plc點(diǎn)位的數(shù)據(jù)代碼實(shí)現(xiàn)

    springboot?通過(guò)博途獲取plc點(diǎn)位的數(shù)據(jù)代碼實(shí)現(xiàn)

    這篇文章主要介紹了springboot?通過(guò)博途獲取plc點(diǎn)位的數(shù)據(jù)的代碼實(shí)現(xiàn),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-08-08
  • Java基礎(chǔ)教程之對(duì)象引用

    Java基礎(chǔ)教程之對(duì)象引用

    這篇文章主要介紹了Java基礎(chǔ)教程之對(duì)象引用,“對(duì)象引用”(object reference)是一個(gè)重要重要概念,涉及內(nèi)存,需要的朋友可以參考下
    2014-09-09

最新評(píng)論