下面列出了org.springframework.core.annotation.AnnotatedElementUtils#getMergedAnnotationAttributes ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void registerBeanDefinitions(AnnotationMetadata metadata,
BeanDefinitionRegistry registry) {
AnnotationAttributes attrs = AnnotatedElementUtils.getMergedAnnotationAttributes(
ClassUtils.resolveClassName(metadata.getClassName(), null),
EnableBinding.class);
for (Class<?> type : collectClasses(attrs, metadata.getClassName())) {
if (!registry.containsBeanDefinition(type.getName())) {
BindingBeanDefinitionRegistryUtils.registerBindingTargetBeanDefinitions(
type, type.getName(), registry);
BindingBeanDefinitionRegistryUtils
.registerBindingTargetsQualifiedBeanDefinitions(ClassUtils
.resolveClassName(metadata.getClassName(), null), type,
registry);
}
}
}
@Test
public void test(){
//RestApiClient需要加@Inherited
AnnotationAttributes annoAttr = AnnotatedElementUtils.getMergedAnnotationAttributes(SubTestClass.class, RestApiClient.class);
assertThat(annoAttr).isNull();
//不需要加@Inherited
RestApiClient anno = AnnotationUtils.findAnnotation(SubTestClass.class, RestApiClient.class);
assertThat(anno).isNotNull();
anno = AnnotationUtils.findAnnotation(SubTestClass2.class, RestApiClient.class);
assertThat(anno).isNotNull();
assertThat(anno.name()).isEqualTo("RestApiClientTestContainerName");
assertThat(anno.url()).isEqualTo("http://RestApiClientTestContainer");
}
@Override
@Nullable
public Map<String, Object> getAnnotationAttributes(String annotationName, boolean classValuesAsString) {
if (this.nestedAnnotationsAsMap) {
return AnnotationMetadata.super.getAnnotationAttributes(annotationName, classValuesAsString);
}
return AnnotatedElementUtils.getMergedAnnotationAttributes(
getIntrospectedClass(), annotationName, classValuesAsString, false);
}
@Override
@Nullable
public Map<String, Object> getAnnotationAttributes(String annotationName, boolean classValuesAsString) {
if (this.nestedAnnotationsAsMap) {
return MethodMetadata.super.getAnnotationAttributes(annotationName, classValuesAsString);
}
return AnnotatedElementUtils.getMergedAnnotationAttributes(this.introspectedMethod,
annotationName, classValuesAsString, false);
}
@Override
@Nullable
public TransactionAttribute parseTransactionAnnotation(AnnotatedElement element) {
AnnotationAttributes attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(
element, javax.transaction.Transactional.class);
if (attributes != null) {
return parseTransactionAnnotation(attributes);
}
else {
return null;
}
}
/**
* Determine a suggested value from any of the given candidate annotations.
*/
@Nullable
protected Object findValue(Annotation[] annotationsToSearch) {
if (annotationsToSearch.length > 0) { // qualifier annotations have to be local
AnnotationAttributes attr = AnnotatedElementUtils.getMergedAnnotationAttributes(
AnnotatedElementUtils.forAnnotations(annotationsToSearch), this.valueAnnotationType);
if (attr != null) {
return extractValue(attr);
}
}
return null;
}
@Override
@Nullable
public TransactionAttribute parseTransactionAnnotation(AnnotatedElement element) {
AnnotationAttributes attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(
element, javax.transaction.Transactional.class);
if (attributes != null) {
return parseTransactionAnnotation(attributes);
}
else {
return null;
}
}
private AnnotationAttributes findAutowiredAnnotation(AccessibleObject ao) {
if (ao.getAnnotations().length > 0) {
for (Class<? extends Annotation> type : this.autowiredAnnotationTypes) {
AnnotationAttributes attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(ao, type);
if (attributes != null) {
return attributes;
}
}
}
return null;
}
/**
* Determine a suggested value from any of the given candidate annotations.
*/
@Nullable
protected Object findValue(Annotation[] annotationsToSearch) {
if (annotationsToSearch.length > 0) { // qualifier annotations have to be local
AnnotationAttributes attr = AnnotatedElementUtils.getMergedAnnotationAttributes(
AnnotatedElementUtils.forAnnotations(annotationsToSearch), this.valueAnnotationType);
if (attr != null) {
return extractValue(attr);
}
}
return null;
}
@Override
public TransactionAttribute parseTransactionAnnotation(AnnotatedElement ae) {
AnnotationAttributes attributes =
AnnotatedElementUtils.getMergedAnnotationAttributes(ae, javax.transaction.Transactional.class);
if (attributes != null) {
return parseTransactionAnnotation(attributes);
}
else {
return null;
}
}
@Override
public TransactionAttribute parseTransactionAnnotation(AnnotatedElement ae) {
AnnotationAttributes attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(ae, Transactional.class);
if (attributes != null) {
return parseTransactionAnnotation(attributes);
}
else {
return null;
}
}
public static Optional<AnnotationAttributes> getAnnotationAttributes(Method method, Class<? extends Annotation> annoClass, boolean searchOnClass){
AnnotationAttributes attrs = null;
if(searchOnClass){
attrs = AnnotatedElementUtils.getMergedAnnotationAttributes(method, annoClass);
if(attrs==null){
Class<?> clazz = method.getDeclaringClass();
attrs = AnnotatedElementUtils.getMergedAnnotationAttributes(clazz, annoClass);
}
}else{
attrs = AnnotatedElementUtils.getMergedAnnotationAttributes(method, annoClass);
}
return Optional.ofNullable(attrs);
}
/**
* Determine a suggested value from any of the given candidate annotations.
*/
protected Object findValue(Annotation[] annotationsToSearch) {
AnnotationAttributes attr = AnnotatedElementUtils.getMergedAnnotationAttributes(
AnnotatedElementUtils.forAnnotations(annotationsToSearch), this.valueAnnotationType);
if (attr != null) {
return extractValue(attr);
}
return null;
}
@Override
public TransactionAttribute parseTransactionAnnotation(AnnotatedElement ae) {
AnnotationAttributes attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(ae, Transactional.class);
if (attributes != null) {
return parseTransactionAnnotation(attributes);
}
else {
return null;
}
}
@Override
@Nullable
public Map<String, Object> getAnnotationAttributes(String annotationName, boolean classValuesAsString) {
return AnnotatedElementUtils.getMergedAnnotationAttributes(this.introspectedMethod,
annotationName, classValuesAsString, this.nestedAnnotationsAsMap);
}
public static void main(String[] args) {
// AnnotatedElement annotatedElement = TransactionalService.class;
AnnotatedElement annotatedElement = TransactionalServiceBean.class;
// 获取 @Service 注解属性独享
AnnotationAttributes serviceAttributes =
AnnotatedElementUtils.getMergedAnnotationAttributes(annotatedElement, Service.class);
// 获取 @Transactional 注解属性独享
AnnotationAttributes transactionalAttributes =
AnnotatedElementUtils.getMergedAnnotationAttributes(annotatedElement, Transactional.class);
// 输出
print(serviceAttributes);
print(transactionalAttributes);
}
private void registerFeignClients(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
List<String> feignClients = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader());
// 如果 spring.factories 里为空
if (feignClients.isEmpty()) {
return;
}
for (String className : feignClients) {
try {
Class<?> clazz = beanClassLoader.loadClass(className);
AnnotationAttributes attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(clazz, FeignClient.class);
if (attributes == null) {
continue;
}
// 如果已经存在该 bean,支持原生的 Feign
if (registry.containsBeanDefinition(className)) {
continue;
}
registerClientConfiguration(registry, getClientName(attributes), attributes.get("configuration"));
validate(attributes);
BeanDefinitionBuilder definition = BeanDefinitionBuilder.genericBeanDefinition(FeignClientFactoryBean.class);
definition.addPropertyValue("url", getUrl(attributes));
definition.addPropertyValue("path", getPath(attributes));
String name = getName(attributes);
definition.addPropertyValue("name", name);
// 兼容最新版本的 spring-cloud-openfeign,尚未发布
StringBuilder aliasBuilder = new StringBuilder(18);
if (attributes.containsKey("contextId")) {
String contextId = getContextId(attributes);
aliasBuilder.append(contextId);
definition.addPropertyValue("contextId", contextId);
} else {
aliasBuilder.append(name);
}
definition.addPropertyValue("type", className);
definition.addPropertyValue("decode404", attributes.get("decode404"));
definition.addPropertyValue("fallback", attributes.get("fallback"));
definition.addPropertyValue("fallbackFactory", attributes.get("fallbackFactory"));
definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);
AbstractBeanDefinition beanDefinition = definition.getBeanDefinition();
// alias
String alias = aliasBuilder.append("FeignClient").toString();
// has a default, won't be null
boolean primary = (Boolean)attributes.get("primary");
beanDefinition.setPrimary(primary);
String qualifier = getQualifier(attributes);
if (StringUtils.hasText(qualifier)) {
alias = qualifier;
}
BeanDefinitionHolder holder = new BeanDefinitionHolder(beanDefinition, className, new String[] { alias });
BeanDefinitionReaderUtils.registerBeanDefinition(holder, registry);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
@Override
public Map<String, Object> getAnnotationAttributes(String annotationName, boolean classValuesAsString) {
return (this.annotations.length > 0 ? AnnotatedElementUtils.getMergedAnnotationAttributes(
getIntrospectedClass(), annotationName, classValuesAsString, this.nestedAnnotationsAsMap) : null);
}
@Override
public Map<String, Object> getAnnotationAttributes(String annotationName, boolean classValuesAsString) {
return (this.annotations.length > 0 ? AnnotatedElementUtils.getMergedAnnotationAttributes(
getIntrospectedClass(), annotationName, classValuesAsString, this.nestedAnnotationsAsMap) : null);
}
@Test
public void testInterceptableCombine1(){
Annotation[] annos = InterceptableCombine1.class.getAnnotations();
List<Annotation> inters = Stream.of(annos).filter(anno->{
/*String clsName = anno.annotationType().getName();
MetadataReader meta;
try {
meta = metadataReaderFactory.getMetadataReader(clsName);
} catch (Exception e) {
throw new BaseException("error", e);
}
boolean annoted = meta.getAnnotationMetadata().isAnnotated(Interceptor.class.getName());*/
// Interceptor inter = anno.annotationType().getAnnotation(Interceptor.class);
boolean res = anno.annotationType().isAnnotationPresent(InterceptorTest.class);
return res;
})
.collect(Collectors.toList());
assertThat(inters).isNotEmpty();
Annotation combina = inters.get(0);
assertThat(combina).isInstanceOf(CombinationMVCInterceptor.class);
//查找包含了@Interceptor注解的注解
List<Annotation> combines = AnnotationUtils.findCombineAnnotations(InterceptableCombine1.class, InterceptorTest.class);
assertThat(combines).isNotEmpty();
//combines.get(0).annotationType() = @CombinationMVCInterceptor
Class<?> combinationMVCInterceptorClass = combines.get(0).annotationType();
assertThat(combinationMVCInterceptorClass).isEqualTo(CombinationMVCInterceptor.class);
//@CombinationMVCInterceptor注解虽然包含了@Interceptor注解,但是因为是可重复的,直接查找@Interceptor是找不到的
AnnotationAttributes attrs = AnnotatedElementUtils.getMergedAnnotationAttributes(combinationMVCInterceptorClass, InterceptorTest.class);
assertThat(attrs).isNotNull();
//其实还可以直接在使用了@CombinationMVCInterceptor注解的类上面直接查找@Interceptor
//但是会包含合并了的父类的注解,前提是子类和父类使用了不同的注解,如InterceptableCombine1类使用了包含@Interceptor的组合注解,而父类直接使用了@Interceptor注解
attrs = AnnotatedElementUtils.getMergedAnnotationAttributes(InterceptableCombine1.class, InterceptorTest.class);
assertThat(attrs).isNotNull();
assertThat(attrs.getClass("value")).isEqualTo(Combine1MvcInterceptor.class);
//根据注解先后顺序只获取到第一个。。。
attrs = AnnotatedElementUtils.getMergedAnnotationAttributes(InterceptableCombine2.class, InterceptorTest.class);
assertThat(attrs).isNotNull();
assertThat(attrs.getClass("value")).isEqualTo(Combine2MvcInterceptor.class);
}