欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring boot 多环境配置

程序员文章站 2022-05-08 12:51:10
...

首先在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里进行配置)

spring boot 多环境配置

在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>