@Profile和@Bean一起使用,决定要不要创建这个bean。
先来看一下@Profile的源码:
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(ProfileCondition.class)
public @interface Profile {
/**
* The set of profiles for which the annotated component should be registered.
*/
String[] value();
}
可以看到,@Profile实际上是一个@Conditional注解,那@Conditiional是干嘛的。
@Conditinal是一个条件注解,参数是一个class,这个class都要实现Condition接口,重写matches()方法。例如,上面示例代码中的ProfileCondition.class
class ProfileCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
if (context.getEnvironment() != null) {
MultiValueMap<String, Object> attrs = metadata.getAllAnnotationAttributes(Profile.class.getName());
if (attrs != null) {
for (Object value : attrs.get("value")) {
if (context.getEnvironment().acceptsProfiles(((String[]) value))) {
return true;
}
}
return false;
}
}
return true;
}
}
mathces()方法的返回值是一个布尔值,返回true时,spring就会创建这个bean,返回false时就不会创建。mathes()方法上有两个参数,分别是ConditionContext和AnnotatedTypeMetadata,这连个参数中包含了大量的信息,ConditionContext中有Environment, ClassLoader等信息,AnnotatedTypeMetadata可以获得注解的信息,具体就不再细说。
接着说@Profile的实现原理。从ProfileCondition的mathes()方法可以看出,Spring从ConditionContext中拿到激活的Profile和注解上的字符串进行比对,以决定是否实例化这个bean。