欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

maven中profile的使用

 更新時間:2023年04月28日 11:26:23   作者:天道有情戰(zhàn)天下  
本文主要介紹了maven中profile的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

一個項目可能會有不同的環(huán)境,例如dev/stating/prod等,不同的環(huán)境的配置文件是不同的,如何根據(jù)環(huán)境快速的切換到對應的配置文件很重要。

在maven和 spring中都有一個profile的概念,下面分別說

一. 在maven中的pom文件中的profile的作用是根據(jù)不同的環(huán)境將對應的環(huán)境變量設置到項目中,如下例子

步驟一:在pom文件中寫profile

<profiles>
        <profile>
            <!-- 開發(fā)環(huán)境 -->
            <id>local</id>
            <properties>
                <env>local</env>
                <profileName>local</profileName>
                <group>LOCAL_GROUP</group>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <!-- 聯(lián)調開發(fā)環(huán)境 -->
            <id>dev</id>
            <properties>
                <env>dev</env>
                <profileName>dev</profileName>
                <group>DEV_GROUP</group>
            </properties>
        </profile>
        <profile>
            <!-- 測試環(huán)境 -->
            <id>test</id>
            <properties>
                <env>test</env>
                <profileName>test</profileName>
                <group>TEST_GROUP</group>
            </properties>
        </profile>
        <profile>
            <!-- 預發(fā)環(huán)境  -->
            <id>pre</id>
            <properties>
                <env>pre</env>
                <profileName>pre</profileName>
                <group>PRE_GROUP</group>
            </properties>
        </profile>
        <profile>
            <!-- 生產環(huán)境  -->
            <id>prod</id>
            <properties>
                <env>prod</env>
                <profileName>prod</profileName>
                <group>PROD_GROUP</group>
            </properties>
        </profile>
    </profiles>

步驟二:設置 resources的位置和filtering為true

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

filtering屬性為true時,主要用來替換項目中資源文件(.xml、.properties)當中由 ${...} 標記的變量

步驟三:激活profile

以下兩種方式可以激活:

a. mvn package時使用-P參數(shù)來指定profile,例如mvn package -P dev,這樣就會把dev 這個profile中的變量設置到項目對應的變量里面,通常是通過${}和@@來注入屬性

b.在對應的profile標簽里面寫入如下標簽,代表默認激活的profile  <activeByDefault>true</activeByDefault> 

二. spring中的profile

Spring中有個注解叫做@Profile("dev"),這個注解只有在對應的profile激活才能使用,這個profile和上面的maven中的沒關系,可以通過如下方式激活

一、啟動Java應用時,通過-D傳入系統(tǒng)參數(shù)

-Dspring.profiles.active=dev  

二、如果是web環(huán)境,可以通過上下文初始化參數(shù)設置

<context-param>  
    <param-name>spring.profiles.active</param-name>  
    <param-value>dev</param-value>  
</context-param>  

三 、通過自定義添加PropertySource

Map<String, Object> map = new HashMap<String, Object>();  
map.put("spring.profiles.active", "dev");  
MapPropertySource propertySource = new MapPropertySource("map", map);  
env.getPropertySources().addFirst(propertySource);  

四、直接設置Profile

env.setActiveProfiles("dev", "test"); 

五、通過操作系統(tǒng)的環(huán)境變量來激活

在Spring Boot項目中,如果事先在OS環(huán)境變量中定義了“SPRING_PROFILES_ACTIVE”,則會采用此處定義的配置文件

以上方式都可以設置多個profile,多個之間通過如逗號/分號等分隔。 

到此這篇關于maven中profile的使用的文章就介紹到這了,更多相關maven profile內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論