@PropertySource注解根据不同环境动态加载自定义配置文件

1、自定义配置文件common-as-dev.properties

stu.name=zhangsan
stu.age=18
stu.sex=man

2、pom里配置环境

<profile>
			<id>as-dev</id>
			<properties>
				<port>8081</port>
				<ctx>/dev</ctx>
				<env>as-dev</env>
			</properties>
			
			<activation>
				<activeByDefault>true</activeByDefault>
			</activation>
		
		</profile>

3、application.properties里引用环境

spring.profiles.active=@env@
server.port=@port@
server.servlet.context-path=@ctx@

4、@PropertySource加载

@PropertySource("classpath:/common-${spring.profiles.active}.properties")
@Configuration
public class LoadProperties {

}

5、@Value注解引用

@Value("${stu.name}")
	private String stuName;
发布了89 篇原创文章 · 获赞 67 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/xl_1803/article/details/99647366
今日推荐