5.Spring高级装配(根据profile的激活状态决定使用的环境) 以及 条件化 Bean

5.1 使用 @Profile

  指定某个 bean 属于哪一个 profile 挒:

@Configuration
@Profile("dev")
public class A{
  ...  
}

Spring 在确定哪个 profile 处于激活状态时、需要依赖两个独立的属性: spring.profile.active 和 spring.profile.default

优先查找active的属性、如果没有就查找default的属性、如果均未设置,只能创建没有激活的profile

5.2设置方式

  • 作为DispatcherServlet 的初始化参数
  • 作为 Web 应用上下文参数
  • 作为 JNDI 条目
  • 作为环境变量
  • 作为JVM的系统属性
  • 在集成测试类上、使用 @ActiveProfiles 注解设置

 5.3 条件化Bean

  @Conditional

原文地址:https://www.cnblogs.com/black-/p/9071817.html