配置文件层次:
pom.xml
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 5 <groupId>com.yanwu.www</groupId> 6 <artifactId>Spring-Env</artifactId> 7 <version>0.0.1-SNAPSHOT</version> 8 <packaging>jar</packaging> 9 10 <name>example</name> 11 <url>http://maven.apache.org</url> 12 13 <parent> 14 <groupId>org.springframework.boot</groupId> 15 <artifactId>spring-boot-starter-parent</artifactId> 16 <version>1.5.6.RELEASE</version> 17 <relativePath/> <!-- lookup parent from repository --> 18 </parent> 19 20 <properties> 21 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 22 </properties> 23 24 <dependencies> 25 <dependency> 26 <groupId>junit</groupId> 27 <artifactId>junit</artifactId> 28 <version>3.8.1</version> 29 <scope>test</scope> 30 </dependency> 31 <!-- web --> 32 <dependency> 33 <groupId>org.springframework.boot</groupId> 34 <artifactId>spring-boot-starter-web</artifactId> 35 </dependency> 36 </dependencies> 37 38 <profiles> 39 <profile> 40 <!-- 本地开发环境 --> 41 <id>dev</id> 42 <properties> 43 <profiles.active>dev</profiles.active> 44 </properties> 45 <activation> 46 <activeByDefault>true</activeByDefault> 47 </activation> 48 </profile> 49 <profile> 50 <!-- 测试环境 --> 51 <id>sit</id> 52 <properties> 53 <profiles.active>sit</profiles.active> 54 </properties> 55 </profile> 56 <profile> 57 <!-- 生产环境 --> 58 <id>pro</id> 59 <properties> 60 <profiles.active>pro</profiles.active> 61 </properties> 62 </profile> 63 64 </profiles> 65 66 <build> 67 <filters> 68 <filter>src/main/resources/env/${profiles.active}/application.yml</filter> 69 </filters> 70 <!-- 替换${key}内容 --> 71 <resources> 72 <resource> 73 <filtering>true</filtering> 74 <!--要到达最底层目录--> 75 <directory>src/main/resources/env/${profiles.active}</directory> 76 </resource> 77 </resources> 78 </build> 79 80 81 82 83 84 </project>