springboot 开发部署指定不同的配置文件

1.pom文件

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.2.RELEASE</version>
	</parent>

	<dependencies>
		<!--Spring Boot -->
		<!--支持 Web 应用开发,包含 Tomcat 和 spring-mvc。 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>
	
	<profiles>
		<profile>
			<!-- 生产环境 -->
			<id>prod</id>
			<properties>
				<activefile>prod</activefile>
			</properties>
		</profile>
		<profile>
			<!-- 本地开发环境 -->
			<id>dev</id>
			<properties>
				<activefile>dev</activefile>
			</properties>
			<activation>
				<activeByDefault>true</activeByDefault>
			</activation>
		</profile>
	</profiles>

	<build>
		<finalName>chatTest</finalName>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

profiles标签下可配置多个配置文件,默认配置文件添加activation标签,值为ture

2.配置文件

src/main/resources/application.properties(汇总配置文件 用以配置不同环境下使用不同的配置文件)

spring.profiles.active=@activefile@
server.port=8081

src/main/resources/application-dev.properties(开发环境下的配置文件)

profilename=dev

src/main/resources/application-prod.properties(生产环境下的配置文件)

profilename=pro

3.java代码

@Value("${profilename}")
public String profilename;

@PostConstruct
public void init(){
		
	Logger log=Logger.getLogger("testappend");
	log.info(profilename);
		
}

4.打包执行

生产环境下

mvn package -P prod

执行效果

2018-07-23 11:17:03.810  INFO 8668 --- [           main] com.knife.test.App                       : The following profiles are active: prod
2018-07-23 11:17:05.576  INFO 8668 --- [           main] testappend                               : pro

开发环境下

mvn package -P dev

执行效果

2018-07-23 11:17:03.810  INFO 8668 --- [           main] com.knife.test.App                       : The following profiles are active: dev
2018-07-23 11:17:05.576  INFO 8668 --- [           main] testappend                               : dev

此时 不同的环境下打包只需改变-P的参数即可 不用在同一份配置文件里改来改去了

猜你喜欢

转载自blog.csdn.net/caideb/article/details/81164347
今日推荐