1. 程式人生 > >spring boot 多環境配置

spring boot 多環境配置

首先在pom.xml中配置profile

      <profile>
            <id>dev</id>
            <properties>
                <profileActive>dev</profileActive>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <profileActive>test</profileActive>
            </properties>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
        </profile>

然後根據不同的環境編寫不同的配置檔案(說明一下:application-dev.yml是本地開發配置檔案,application為springboot的主配置檔案application.yml的名字,dev為開發環境,在pom.xml的profile裡進行配置)

在pom.xml中根據環境獲取不同的配置檔案(注意:${profileActive}要和<profileActive>一樣

           <resource>
                <directory>src/main/resources/environment/${profileActive}</directory>
                <excludes>
                    <exclude>environment/${profileActive}/${profileActive}.properties</exclude>
                </excludes>
            </resource>

在讀取配置檔案的時候要先排除環境下的檔案

<!--指定資源的位置 -->
	    <resource>
		<directory>src/main/resources</directory>
                    <filtering>true</filtering>
                    <excludes>
                        <exclude>environment/**</exclude>
                    </excludes>
            </resource>

dev.properties的內容是

profile.active=dev

在application.yml裡需要這樣配置

spring:
    profiles:
        active: '@[email protected]'
        include: email,xmyb
mybatis:
    mapperLocations: classpath:cn/linkengine/pre/service/aiom/mapper/*.xml
    configLocation: classpath:mybatis.xml

注意:1.'@[email protected]'必須加單引號或雙引號,高版本springboot是用@[email protected]的,低版本是${},且必須和properties檔案裡的key一樣。

2.mybatis.xml只要classpath:mybatis.xml即可引入,因為或根據不同的環境把mybatis.xml引入到配置檔案的根目錄下

最後要在pom.xml裡把properties檔案給filter(注意:必須在讀取配置檔案的時候<filtering>true</filtering>)

       <filters>
            <filter>src/main/resources/environment/${profileActive}/${profileActive}.properties</filter>
        </filters>

整個pom.xml內容為

        <build>
                <filters>
                    <filter>src/main/resources/environment/${profileActive}/${profileActive}.properties</filter>
                </filters>
		<resources>
			<!--指定資源的位置 -->
			<resource>
				<directory>src/main/resources</directory>
                                <filtering>true</filtering>
                                <excludes>
                                    <exclude>environment/**</exclude>
                                </excludes>
                        </resource>
                        <resource>
                            <directory>src/main/resources/environment/${profileActive}</directory>
                            <excludes>
                                <exclude>environment/${profileActive}/${profileActive}.properties</exclude>
                            </excludes>
                        </resource>
		</resources>
        </build>
<profiles>
        <profile>
            <id>dev</id>
            <properties>
                <profileActive>dev</profileActive>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <profileActive>test</profileActive>
            </properties>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
        </profile>
</profiles>