### 1. Maven的profile配置
pom.xml:
```XML
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>prod</id>
</profile>
</profiles>
```
如上是maven的最基本profile配置,<activeByDefault>的作用是不指定profile时默认dev。当然,只是这样没有任何用处。
往往还需要在<build>标签中加入<resources>标签来配置在不同profile下的资源文件的加载策略。
**下面展示一个比较完整的配置。**
现在需要在dev环境下,读取resource目录下的通用配置以及dev目录下的配置;prod环境下,读取读取resource目录下的通用配置以及prod目录下的配置。
资源文件目录结构:

对应的pom配置:
```XML
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>profile/**</exclude>
</excludes>
</resource>
</resources>
</build>
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<resources>
<resource>
<directory>src/main/resources/profile/dev</directory>
</resource>
</resources>
</build>
</profile>
<profile>
<id>prod</id>
<build>
<resources>
<resource>
<directory>src/main/resources/profile/prod</directory>
</resource>
</resources>
</build>
</profile>
</profiles>
```
如上所示,在<build>标签中指定了手动指定了资源文件夹,同时将profile目录下的全部文件去除。之后再在profile中加载自身的配置目录。
### 2. SpringBoot的profile配置
SpringBoot的profile,可以通过修改spring的配置文件来实现。每次切换都要手动更改对应的环境。
dev环境的配置application-dev.properties:
```PROPERTIES
spring.profiles = dev
```
在application.properties配置文件中加入:
```PROPERTIES
spring.profiles.active = dev
```
则启动时profile为dev。也可以通过在启动时添加下面的参数来指定profile:
```BASH
java -jar SpringBootDemo.jar --spring.profiles.active=dev
```
如果想根据不同profile加载不同bean,可以在对应类上加@profile注解:
```JAVA
/**
* 非dev环境加载本bean
*/
@Service
@Profile("!dev")
public class BeanClass {
}
```
### 3.Maven+SpringBoot
maven和SpringBoot配合使用,是通过Maven构建时替换Spring配置文件中的占位符来实现。
pom.xml:
```XML
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<!-- 替换资源文件中的占位符: -->
<filtering>true</filtering>
<excludes>
<exclude>profile/**</exclude>
</excludes>
</resource>
</resources>
</build>
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<!-- 定义当前profile的properties -->
<properties>
<active.target.profile>dev</active.target.profile>
</properties>
<build>
<resources>
<resource>
<directory>src/main/resources/profile/dev</directory>
</resource>
</resources>
</build>
</profile>
<profile>
<id>prod</id>
<!-- 定义当前profile的properties -->
<properties>
<active.target.profile>prod</active.target.profile>
</properties>
<build>
<resources>
<resource>
<directory>src/main/resources/profile/prod</directory>
</resource>
</resources>
</build>
</profile>
</profiles>
```
application.properties:
```PROPERTIES
spring.profiles.active = @active.target.profile@
```
以上。
SpringBoot和Maven的Profile配置