类org.springframework.core.type.StandardMethodMetadata源码实例Demo

下面列出了怎么用org.springframework.core.type.StandardMethodMetadata的API类实例代码及写法,或者点击链接到github查看源代码。

@Override
public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition,
		Class<?> beanType, String beanName) {
	if (checkSentinelProtect(beanDefinition, beanType, beanName)) {
		SentinelRestTemplate sentinelRestTemplate;
		if (beanDefinition.getSource() instanceof StandardMethodMetadata) {
			sentinelRestTemplate = ((StandardMethodMetadata) beanDefinition
					.getSource()).getIntrospectedMethod()
							.getAnnotation(SentinelRestTemplate.class);
		}
		else {
			sentinelRestTemplate = beanDefinition.getResolvedFactoryMethod()
					.getAnnotation(SentinelRestTemplate.class);
		}
		// check class and method validation
		checkSentinelRestTemplate(sentinelRestTemplate, beanName);
		cache.put(beanName, sentinelRestTemplate);
	}
}
 
源代码2 项目: saluki   文件: TestController.java
private Collection<Object> getTypedBeansWithAnnotation(Class<? extends Annotation> annotationType)
    throws Exception {
  return Stream.of(applicationContext.getBeanNamesForAnnotation(annotationType)).filter(name -> {
    BeanDefinition beanDefinition = applicationContext.getBeanFactory().getBeanDefinition(name);
    if (beanDefinition.getSource() instanceof StandardMethodMetadata) {
      StandardMethodMetadata metadata = (StandardMethodMetadata) beanDefinition.getSource();
      return metadata.isAnnotated(annotationType.getName());
    }
    return null != applicationContext.getBeanFactory().findAnnotationOnBean(name, annotationType);
  }).map(name -> applicationContext.getBeanFactory().getBean(name)).collect(Collectors.toList());

}
 
源代码3 项目: saluki   文件: GrpcServiceRunner.java
private Collection<Object> getTypedBeansWithAnnotation(Class<? extends Annotation> annotationType)
    throws Exception {
  return Stream.of(applicationContext.getBeanNamesForAnnotation(annotationType)).filter(name -> {
    BeanDefinition beanDefinition = applicationContext.getBeanFactory().getBeanDefinition(name);
    if (beanDefinition.getSource() instanceof StandardMethodMetadata) {
      StandardMethodMetadata metadata = (StandardMethodMetadata) beanDefinition.getSource();
      return metadata.isAnnotated(annotationType.getName());
    }
    return null != applicationContext.getBeanFactory().findAnnotationOnBean(name, annotationType);
  }).map(name -> applicationContext.getBeanFactory().getBean(name)).collect(Collectors.toList());

}
 
private boolean isBeanWithGemfireTemplateDependency(@NonNull BeanFactory beanFactory,
		@NonNull BeanDefinition beanDefinition) {

	Predicate<Object> isGemfireTemplate = value -> value instanceof RuntimeBeanReference
		? beanFactory.isTypeMatch(((RuntimeBeanReference) value).getBeanName(), GemfireOperations.class)
		: value instanceof GemfireOperations;

	boolean match = beanDefinition.getConstructorArgumentValues().getGenericArgumentValues().stream()
		.map(ConstructorArgumentValues.ValueHolder::getValue)
		.anyMatch(isGemfireTemplate);

	match |= match || beanDefinition.getPropertyValues().getPropertyValueList().stream()
		.map(PropertyValue::getValue)
		.anyMatch(isGemfireTemplate);

	match |= match || Optional.of(beanDefinition)
		.filter(AnnotatedBeanDefinition.class::isInstance)
		.map(AnnotatedBeanDefinition.class::cast)
		.map(AnnotatedBeanDefinition::getFactoryMethodMetadata)
		.filter(StandardMethodMetadata.class::isInstance)
		.map(StandardMethodMetadata.class::cast)
		.map(StandardMethodMetadata::getIntrospectedMethod)
		.map(method -> Arrays.stream(ArrayUtils.nullSafeArray(method.getParameterTypes(), Class.class))
			.filter(Objects::nonNull)
			.anyMatch(GemfireOperations.class::isAssignableFrom)
		).orElse(false);

	return match;
}
 
private String getQualifier(String key) {
	if (this.applicationContext != null && this.applicationContext.getBeanFactory().containsBeanDefinition(key)) {
		BeanDefinition beanDefinition = this.applicationContext.getBeanFactory().getBeanDefinition(key);
		Object source = beanDefinition.getSource();
		if (source instanceof StandardMethodMetadata) {
			StandardMethodMetadata metadata = (StandardMethodMetadata) source;
			Qualifier qualifier = AnnotatedElementUtils.findMergedAnnotation(metadata.getIntrospectedMethod(),
				Qualifier.class);
			if (qualifier != null && qualifier.value().length() > 0) {
				return qualifier.value();
			}
		}
	}
	return key;
}
 
private <T> T findQualifiedBeanByType(Class<? extends T> type, String qualifierValue, Class<? extends Annotation> qualifierType) {
    final NoSuchBeanDefinitionException noSuchBeanDefinitionException = new NoSuchBeanDefinitionException(qualifierValue, "No matching " + type.getSimpleName() +
            " bean found for qualifier " + qualifierValue + " of type " + qualifierType.getSimpleName() + " !");
    try {

        if (StringUtils.isEmpty(qualifierValue)) {
            if (qualifierType.equals(Qualifier.class)) {
                return Optional.of(
                        context.getBean(type))
                        .orElseThrow(() -> noSuchBeanDefinitionException);
            }
            return context.getBean(
                    Arrays.stream(context.getBeanNamesForAnnotation(qualifierType))
                            .filter(beanName -> type.isInstance(context.getBean(beanName)))
                            .findFirst()
                            .orElseThrow(() -> noSuchBeanDefinitionException),
                    type);
        }

        return BeanFactoryAnnotationUtils.qualifiedBeanOfType(context.getBeanFactory(), type, qualifierValue);
    } catch (NoSuchBeanDefinitionException noBeanException) {
        ConfigurableListableBeanFactory factory = context.getBeanFactory();

        for (String name : factory.getBeanDefinitionNames()) {
            BeanDefinition bd = factory.getBeanDefinition(name);

            if (bd.getSource() instanceof StandardMethodMetadata) {
                StandardMethodMetadata metadata = (StandardMethodMetadata) bd.getSource();

                if (metadata.getReturnTypeName().equals(type.getName())) {
                    Map<String, Object> attributes = metadata.getAnnotationAttributes(qualifierType.getName());
                    if (null != attributes) {
                        if (qualifierType.equals(Qualifier.class)) {
                            if (qualifierValue.equals(attributes.get("value"))) {
                                return context.getBean(name, type);
                            }
                        }
                        return context.getBean(name, type);
                    }
                }
            }
        }

        throw noSuchBeanDefinitionException;
    }
}
 
@SuppressWarnings({"unchecked"})
private List<SpqrBean> findGraphQLApiBeans() {
    ConfigurableListableBeanFactory factory = context.getBeanFactory();

    List<SpqrBean> spqrBeans = new ArrayList<>();

    for (String beanName : factory.getBeanDefinitionNames()) {
        BeanDefinition bd = factory.getBeanDefinition(beanName);

        if (bd.getSource() instanceof StandardMethodMetadata) {
            StandardMethodMetadata metadata = (StandardMethodMetadata) bd.getSource();

            Map<String, Object> attributes = metadata.getAnnotationAttributes(GraphQLApi.class.getName());
            if (null == attributes) {
                continue;
            }

            SpqrBean spqrBean = new SpqrBean(context, beanName, metadata.getIntrospectedMethod().getAnnotatedReturnType());

            Map<String, Object> withResolverBuildersAttributes = metadata.getAnnotationAttributes(WithResolverBuilders.class.getTypeName());
            if (withResolverBuildersAttributes != null) {
                AnnotationAttributes[] annotationAttributesArray = (AnnotationAttributes[]) withResolverBuildersAttributes.get("value");
                Arrays.stream(annotationAttributesArray)
                        .forEach(annotationAttributes ->
                                spqrBean.getResolverBuilders().add(
                                        new ResolverBuilderBeanIdentity(
                                                (Class<? extends ResolverBuilder>) annotationAttributes.get("value"),
                                                (String) annotationAttributes.get("qualifierValue"),
                                                (Class<? extends Annotation>) annotationAttributes.get("qualifierType"))
                                )
                        );
            } else {
                Map<String, Object> withResolverBuilderAttributes = metadata.getAnnotationAttributes(WithResolverBuilder.class.getTypeName());
                if (withResolverBuilderAttributes != null) {
                    spqrBean.getResolverBuilders().add(
                            new ResolverBuilderBeanIdentity(
                                    (Class<? extends ResolverBuilder>) withResolverBuilderAttributes.get("value"),
                                    (String) withResolverBuilderAttributes.get("qualifierValue"),
                                    (Class<? extends Annotation>) withResolverBuilderAttributes.get("qualifierType"))
                    );
                }
            }

            spqrBeans.add(spqrBean);
        }
    }

    return spqrBeans;
}
 
 类所在包
 同包方法