SpringFrame的版本5.0.9.release。java
咱們會使用@Profile來分開開發環境和生產環境,Profile是如何實現的呢,如List-1,注意@Conditional的value是ProfileConditiongit
List-1github
@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(); }
以下List-2,ProfileCondition實現了Condition接口,重點在於matches方法,得到Profile的value值,以後用Environment的acceptsProfiles方法判斷是不是能夠接受的profile。spring
List-2ide
package org.springframework.context.annotation; import org.springframework.core.type.AnnotatedTypeMetadata; import org.springframework.util.MultiValueMap; /** * @author Chris Beams * @author Phillip Webb * @author Juergen Hoeller * @since 4.0 */ class ProfileCondition implements Condition { @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { 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; } }
引出一個問題,這個ProfileCondition是什麼時候被調用的呢,這個就要了解@Conditional這個註解了,看個人另外一篇文章。spa