最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用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ò)加載的文件都要寫(xiě)出來(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í)候希望靈活替換。所以就寫(xiě)一個(gè)config文件目錄,里面放過(guò)web.xml,當(dāng)要打包的時(shí)候替換WEB-INF下的web.xml文件就可以了

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

總結(jié)

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

相關(guān)文章

  • Java中的排序與內(nèi)部比較器Compareable解析

    Java中的排序與內(nèi)部比較器Compareable解析

    這篇文章主要介紹了Java中的排序與內(nèi)部比較器Compareable解析,一般沒(méi)有特殊要求時(shí),直接調(diào)用(底層默認(rèn)的升序排列)就可以得到想要的結(jié)果,所謂的 sort 方法排序底層都是基于這兩種排序,故如果需要設(shè)計(jì)成所想要的排序就需要了解底層排序原理,需要的朋友可以參考下
    2023-11-11
  • Spring事務(wù)處理原理步驟詳解

    Spring事務(wù)處理原理步驟詳解

    這篇文章主要介紹了Spring事務(wù)處理原理步驟詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • SpringBoot2.x集成Dozer的示例代碼

    SpringBoot2.x集成Dozer的示例代碼

    本文主要介紹了SpringBoot2.x集成Dozer的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • SpringBoot整合Milvus的實(shí)現(xiàn)

    SpringBoot整合Milvus的實(shí)現(xiàn)

    本文主要介紹了SpringBoot整合Milvus的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • Java垃圾收集之對(duì)象存活判定、回收流程與內(nèi)存策略詳解

    Java垃圾收集之對(duì)象存活判定、回收流程與內(nèi)存策略詳解

    Java對(duì)象的內(nèi)存分配主要發(fā)生在堆內(nèi)存(新生代、老年代),少數(shù)情況可能分配在棧(棧上分配)或直接內(nèi)存(堆外內(nèi)存),這篇文章主要介紹了Java垃圾收集之對(duì)象存活判定、回收流程與內(nèi)存策略的相關(guān)資料,需要的朋友可以參考下
    2026-03-03
  • 在Spring?AI?中配置多個(gè)?LLM?客戶端的詳細(xì)過(guò)程

    在Spring?AI?中配置多個(gè)?LLM?客戶端的詳細(xì)過(guò)程

    本文探討了如何在單個(gè)Spring?AI應(yīng)用中集成多個(gè)LLM,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2025-10-10
  • SpringCloud集成MybatisPlus實(shí)現(xiàn)MySQL多數(shù)據(jù)源配置方法

    SpringCloud集成MybatisPlus實(shí)現(xiàn)MySQL多數(shù)據(jù)源配置方法

    本文詳細(xì)介紹了SpringCloud集成MybatisPlus實(shí)現(xiàn)MySQL多數(shù)據(jù)源配置的方法,包括在application.properties中配置多數(shù)據(jù)源,配置MybatisPlus,創(chuàng)建Mapper接口和使用多數(shù)據(jù)源等步驟,此外,還解釋了每一個(gè)配置項(xiàng)目的含義,以便讀者更好地理解和應(yīng)用
    2024-10-10
  • javaWeb使用驗(yàn)證碼實(shí)現(xiàn)簡(jiǎn)單登錄

    javaWeb使用驗(yàn)證碼實(shí)現(xiàn)簡(jiǎn)單登錄

    這篇文章主要為大家詳細(xì)介紹了javaWeb使用驗(yàn)證碼實(shí)現(xiàn)簡(jiǎn)單登錄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • java獲取當(dāng)前時(shí)間并格式化代碼實(shí)例

    java獲取當(dāng)前時(shí)間并格式化代碼實(shí)例

    這篇文章主要介紹了java獲取當(dāng)前時(shí)間并格式化代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • 在Java的JDBC使用中設(shè)置事務(wù)回滾的保存點(diǎn)的方法

    在Java的JDBC使用中設(shè)置事務(wù)回滾的保存點(diǎn)的方法

    這篇文章主要介紹了在Java的JDBC使用中設(shè)置事務(wù)回滾的保存點(diǎn)的方法,JDBC是Java用于連接各種數(shù)據(jù)庫(kù)的API,需要的朋友可以參考下
    2015-12-12

最新評(píng)論

呈贡县| 呼图壁县| 通榆县| 防城港市| 洞口县| 北川| 永新县| 南雄市| 康马县| 明光市| 靖西县| 盐池县| 天峻县| 龙胜| 大化| 张家川| 静乐县| 陈巴尔虎旗| 平塘县| 泸西县| 湟中县| 三江| 会宁县| 津南区| 高雄县| 富蕴县| 隆尧县| 灵武市| 安陆市| 天长市| 张掖市| 广安市| 九江县| 杭锦后旗| 尉犁县| 平度市| 潍坊市| 达日县| 汤阴县| 岳普湖县| 莆田市|