Java Code Examples for org.springframework.core.annotation.AnnotatedElementUtils#getMergedAnnotationAttributes()

The following examples show how to use org.springframework.core.annotation.AnnotatedElementUtils#getMergedAnnotationAttributes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BindingBeansRegistrar.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
@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);
		}
	}
}
 
Example 2
Source File: AnnotatedElementUtilsTest.java    From onetwo with Apache License 2.0 6 votes vote down vote up
@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");
}
 
Example 3
Source File: StandardMethodMetadata.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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);
}
 
Example 4
Source File: JtaTransactionAnnotationParser.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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;
	}
}
 
Example 5
Source File: QualifierAnnotationAutowireCandidateResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * 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;
}
 
Example 6
Source File: StandardAnnotationMetadata.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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);
}
 
Example 7
Source File: JtaTransactionAnnotationParser.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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;
	}
}
 
Example 8
Source File: AutowiredAnnotationBeanPostProcessor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 9
Source File: QualifierAnnotationAutowireCandidateResolver.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * 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;
}
 
Example 10
Source File: SpringTransactionAnnotationParser.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public TransactionAttribute parseTransactionAnnotation(AnnotatedElement ae) {
	AnnotationAttributes attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(ae, Transactional.class);
	if (attributes != null) {
		return parseTransactionAnnotation(attributes);
	}
	else {
		return null;
	}
}
 
Example 11
Source File: JtaTransactionAnnotationParser.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public TransactionAttribute parseTransactionAnnotation(AnnotatedElement ae) {
	AnnotationAttributes attributes =
			AnnotatedElementUtils.getMergedAnnotationAttributes(ae, javax.transaction.Transactional.class);
	if (attributes != null) {
		return parseTransactionAnnotation(attributes);
	}
	else {
		return null;
	}
}
 
Example 12
Source File: SpringTransactionAnnotationParser.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public TransactionAttribute parseTransactionAnnotation(AnnotatedElement ae) {
	AnnotationAttributes attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(ae, Transactional.class);
	if (attributes != null) {
		return parseTransactionAnnotation(attributes);
	}
	else {
		return null;
	}
}
 
Example 13
Source File: SpringUtils.java    From onetwo with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 14
Source File: QualifierAnnotationAutowireCandidateResolver.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 15
Source File: InterceptorNorepeatTest.java    From onetwo with Apache License 2.0 4 votes vote down vote up
@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);
	}
 
Example 16
Source File: StandardAnnotationMetadata.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public Map<String, Object> getAnnotationAttributes(String annotationName, boolean classValuesAsString) {
	return (this.annotations.length > 0 ? AnnotatedElementUtils.getMergedAnnotationAttributes(
			getIntrospectedClass(), annotationName, classValuesAsString, this.nestedAnnotationsAsMap) : null);
}
 
Example 17
Source File: StandardAnnotationMetadata.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Map<String, Object> getAnnotationAttributes(String annotationName, boolean classValuesAsString) {
	return (this.annotations.length > 0 ? AnnotatedElementUtils.getMergedAnnotationAttributes(
			getIntrospectedClass(), annotationName, classValuesAsString, this.nestedAnnotationsAsMap) : null);
}
 
Example 18
Source File: BladeFeignClientsRegistrar.java    From blade-tool with GNU Lesser General Public License v3.0 4 votes vote down vote up
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();
		}
	}
}
 
Example 19
Source File: AnnotationAttributesBootstrap.java    From thinking-in-spring-boot-samples with Apache License 2.0 4 votes vote down vote up
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);
    }
 
Example 20
Source File: StandardMethodMetadata.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
@Nullable
public Map<String, Object> getAnnotationAttributes(String annotationName, boolean classValuesAsString) {
	return AnnotatedElementUtils.getMergedAnnotationAttributes(this.introspectedMethod,
			annotationName, classValuesAsString, this.nestedAnnotationsAsMap);
}