下面列出了org.springframework.core.type.AnnotatedTypeMetadata#getAnnotationAttributes ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
Map<String, Object> springBootVersion = metadata
.getAnnotationAttributes(ConditionalOnSpringBootVersion.class.getCanonicalName());
if (springBootVersion == null || springBootVersion.get("version") == null) {
return new ConditionOutcome(false, "No specified spring boot version.");
}
ConditionalOnSpringBootVersion.Version version = (ConditionalOnSpringBootVersion.Version) springBootVersion
.get("version");
if (ConditionalOnSpringBootVersion.Version.ANY.equals(version)) {
return new ConditionOutcome(true, "Conditional on Any Spring Boot.");
} else if (ConditionalOnSpringBootVersion.Version.OneX.equals(version)) {
if (SpringBootVersion.getVersion().startsWith("1")) {
return new ConditionOutcome(true, "Conditional on OneX Spring Boot.");
} else {
return new ConditionOutcome(false, "Conditional on OneX Spring Boot.");
}
} else if (ConditionalOnSpringBootVersion.Version.TwoX.equals(version)) {
if (SpringBootVersion.getVersion().startsWith("2")) {
return new ConditionOutcome(true, "Conditional on TwoX Spring Boot.");
} else {
return new ConditionOutcome(false, "Conditional on TwoX Spring Boot.");
}
}
throw new IllegalStateException("Error Spring Boot Version.");
}
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata metadata) {
Map<String, Object> attributes =
metadata.getAnnotationAttributes(ConditionalOnWatsonServiceProperties.class.getName());
String prefix = (String) attributes.get("prefix");
// If the service is specifically marked as enabled, the condition is true
if (Boolean.valueOf(conditionContext.getEnvironment().getProperty(prefix + ".enabled"))) {
return true;
}
// If any of the configuration properties for the service are present, the condition is true
String url = conditionContext.getEnvironment().getProperty(prefix + ".url");
String username = conditionContext.getEnvironment().getProperty(prefix + ".username");
String password = conditionContext.getEnvironment().getProperty(prefix + ".password");
String apiKey = conditionContext.getEnvironment().getProperty(prefix + ".apiKey");
String iamApiKey = conditionContext.getEnvironment().getProperty(prefix + ".iamApiKey");
String versionDate = conditionContext.getEnvironment().getProperty(prefix + ".versionDate");
if (url != null || username != null || password != null || versionDate != null
|| apiKey != null || iamApiKey != null) {
return true;
}
return false;
}
/**
* Determines whether the current runtime environment matches the one passed through the annotation.
* @param context the spring context at the point in time the condition is being evaluated
* @param metadata annotation metadata containing all acceptable GCP environments
* @throws org.springframework.beans.factory.NoSuchBeanDefinitionException if no GcpEnvironmentProvider is found in
* spring context
*/
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
Map<String, Object> attributes = metadata.getAnnotationAttributes(ConditionalOnGcpEnvironment.class.getName());
GcpEnvironment[] targetEnvironments = (GcpEnvironment[]) attributes.get("value");
Assert.notNull(targetEnvironments, "Value attribute of ConditionalOnGcpEnvironment cannot be null.");
GcpEnvironmentProvider environmentProvider = context.getBeanFactory().getBean(GcpEnvironmentProvider.class);
GcpEnvironment currentEnvironment = environmentProvider.getCurrentEnvironment();
if (Arrays.stream(targetEnvironments).noneMatch((env) -> env == currentEnvironment)) {
return new ConditionOutcome(false, "Application is not running on any of "
+ Arrays.stream(targetEnvironments)
.map(GcpEnvironment::toString)
.collect(Collectors.joining(", ")));
}
return new ConditionOutcome(true, "Application is running on " + currentEnvironment);
}
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
Environment env = context.getEnvironment();
Map<String, Object> map = metadata.getAnnotationAttributes(JdbcUrl.class.getName());
if (map != null && map.containsKey("value")) {
String value = map.get("value").toString();
String jdbcUrl = env.getProperty("jdbcUrl");
boolean isEmbedded = Boolean.parseBoolean(env.getProperty("isEmbedded"));
boolean result;
if (value.equals("hsqldb")) {
result = jdbcUrl == null || isEmbedded;
} else if (jdbcUrl == null || isEmbedded) {
result = false;
} else if (!jdbcUrl.startsWith("jdbc:")) {
result = false;
} else {
result = jdbcUrl.substring(5).toLowerCase().startsWith(value.toLowerCase());
}
LOGGER.debug("Returning {} for {}", result, value);
return result;
}
LOGGER.debug("No data provided");
return false;
}
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
if (metadata.isAnnotated(Bean.class.getName())) {
Map<String, Object> map = metadata.getAnnotationAttributes(Bean.class.getName());
if(!CollectionUtils.isEmpty(map)) {
String[] names = (String[]) map.get("name");
if(ArrayUtils.isNotEmpty(names)) {
for(String name : names) {
if(StringUtils.isNotBlank(name) && name.endsWith("ConfigBean")) {
return true;
}
}
}
}
}
return false;
}
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
Map<String, Object> attributes = metadata.getAnnotationAttributes(ConditionalOnEnvironment.class.getName());
Env[] envs = (Env[]) attributes.get("values");
Env currentEnv = Env.getCurrentEnv();
return Sets.newHashSet(envs).contains(currentEnv);
}
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
Map<String, Object> annotationAttributes = metadata.getAnnotationAttributes(MultConditionalOnProperty.
class.getName());
String propertyName = (String) annotationAttributes.get("name");
if (StringUtils.isBlank(propertyName)) {
propertyName = (String) annotationAttributes.get("value");
}
if (StringUtils.isBlank(propertyName)) {
return new ConditionOutcome(false, "没发现配置name或value");
}
String[] values = (String[]) annotationAttributes.get("havingValue");
if (values.length == 0) {
return new ConditionOutcome(false, "没发现配置havingValue");
}
String propertyValue = context.getEnvironment().getProperty(propertyName);
if (StringUtils.isBlank(propertyValue)) {
propertyValue = ((ConfigurableApplicationContext) context.getResourceLoader()).getEnvironment().
getProperty(propertyName);
if (StringUtils.isBlank(propertyValue)) {
return new ConditionOutcome(false, "没发现配置" + propertyName);
}
}
/**
* 相当于或的关系,只要有一个能匹配就算成功
*/
for (String havingValue : values) {
if (propertyValue.equalsIgnoreCase(havingValue)) {
return new ConditionOutcome(true, "匹配成功");
}
}
return new ConditionOutcome(false, "匹配失败");
}
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
Map<String, Object> beanAttributes = metadata.getAnnotationAttributes(Bean.class.getName());
String beanName = ((String[]) beanAttributes.get("name"))[0];
if(StringUtils.isEmpty(beanName)) {
throw new IllegalStateException("OnMissingBeanCondition can't detect bean name!");
}
boolean missingBean = !context.getBeanFactory().containsBean(context.getEnvironment().resolveRequiredPlaceholders(beanName));
return missingBean ? ConditionOutcome.match(beanName + " not found") : ConditionOutcome.noMatch(beanName + " found");
}
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
String implVersion = null;
String wicketVersion = retrieveWicketVersion(implVersion);
Map<String, Object> attributes = metadata
.getAnnotationAttributes(ConditionalOnWicket.class.getName());
Range range = (Range) attributes.get("range");
int expectedVersion = (int) attributes.get("value");
String[] splittedWicketVersion = wicketVersion.split("\\.");
int majorWicketVersion = Integer.valueOf(splittedWicketVersion[0]);
return getMatchOutcome(range, majorWicketVersion, expectedVersion);
}
protected int getParameterIndex(AnnotatedTypeMetadata annotatedTypeMetadata) {
Map<String, Object> params = annotatedTypeMetadata.getAnnotationAttributes(Conditional.class.getName());
return (int) params.get(ConditionalAnnotationMetadata.PARAMETER_INDEX);
}
protected int getParameterIndex(AnnotatedTypeMetadata annotatedTypeMetadata) {
Map<String, Object> params = annotatedTypeMetadata.getAnnotationAttributes(Conditional.class.getName());
return (int) params.get(ConditionalAnnotationMetadata.PARAMETER_INDEX);
}