spring知识之五


Profile

提供在不同的环境下使用不同的配置

激活Profile

Spring在确定哪个profile处于激活状态时,需要依赖两个独立的属性:spring.profiles.active和spring.profiles.default。如果设置了spring.profiles.active属性的话,那么它的值就会用来确定哪个profile是激活的。但如果没有设置spring.profiles.active属性的话,那Spring将会查找spring.profiles.default的值。如果spring.profiles.active和spring.profiles.default均没有设置的话,那就没有激活的profile,因此只会创建那些没有定义在profile中的bean。

使用profile进行测试

当运行集成测试时,通常会希望采用与生产环境(或者是生产环境的部分子集)相同的配置进行测试。但是,如果配置中的bean定义在了profile中,那么在运行测试时,我们就需要有一种方式来启用合适的profile。

Spring提供了@ActiveProfiles注解,我们可以使用它来指定运行测试时要激活哪个profile。在集成测试时,通常想要激活的是开发环境的profile。

比如Profile("dev")

Application Event

使用Application Event可以做到Bean与Bean之间的通信

Spring的事件需要遵循如下流程:

  • 自定义事件,集成ApplicationEvent

  • 定义事件监听器,实现ApplicationListener

  • 使用容器发布事件

猜你喜欢

转载自blog.csdn.net/xywnrf/article/details/80873789
今日推荐