Spring @Profile注解

@Profile注解表示当指定的profiles被激活是,对应的注解的组件可以被spring注册到容器。
注意:
Spring官网的原文:
If a @Configuration class is marked with @Profile, all of the @Bean methods and @Import annotations associated with that class will be bypassed unless one or more of the specified profiles are active. If a @Component or @Configuration class is marked with @Profile({“p1”, “p2”}), that class will not be registered/processed unless profiles ‘p1’ and/or ‘p2’ have been activated. If a given profile is prefixed with the NOT operator (!), the annotated element will be registered if the profile is not active. For example, given @Profile({“p1”, “!p2”}), registration will occur if profile ‘p1’ is active or if profile ‘p2’ is not active.


当@Configuration注解的类被@Profile注解标注后,这个Configuration类里的所有@Bean注解,@Import注解关联的类只有当对应的一个或多个profile被激活时才会被spring处理。
例如:@Component 或者 @Configuration被@Profile({“p1”, “p2”})标注了,那么这个类只有在profile是p1 或者 p2 或者 p1 AND p2时才会被处理,否则不会被spring处理。如果在profile加一个非操作(!)的前缀,那么被@Profile注解的元素将会在指定的profile未被激活的情况下被spring处理。
例如:@Profile({“p1”, “!p2”}),元素在p1被激活或者p2未被激活的情况下才会被spring处理。

你可能感兴趣的:(Spring,spring)