org.springframework.core.annotation.AnnotatedElementUtils#findMergedAnnotationAttributes ( )源码实例Demo

下面列出了org.springframework.core.annotation.AnnotatedElementUtils#findMergedAnnotationAttributes ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

public static String determineBeanNameFor(Method beanMethod) {
	String beanName = beanNameCache.get(beanMethod);
	if (beanName == null) {
		// By default, the bean name is the name of the @Bean-annotated method
		beanName = beanMethod.getName();
		// Check to see if the user has explicitly set a custom bean name...
		AnnotationAttributes bean =
				AnnotatedElementUtils.findMergedAnnotationAttributes(beanMethod, Bean.class, false, false);
		if (bean != null) {
			String[] names = bean.getStringArray("name");
			if (names.length > 0) {
				beanName = names[0];
			}
		}
		beanNameCache.put(beanMethod, beanName);
	}
	return beanName;
}
 
public static String determineBeanNameFor(Method beanMethod) {
	String beanName = beanNameCache.get(beanMethod);
	if (beanName == null) {
		// By default, the bean name is the name of the @Bean-annotated method
		beanName = beanMethod.getName();
		// Check to see if the user has explicitly set a custom bean name...
		AnnotationAttributes bean =
				AnnotatedElementUtils.findMergedAnnotationAttributes(beanMethod, Bean.class, false, false);
		if (bean != null) {
			String[] names = bean.getStringArray("name");
			if (names.length > 0) {
				beanName = names[0];
			}
		}
		beanNameCache.put(beanMethod, beanName);
	}
	return beanName;
}
 
public static boolean isScopedProxy(Method beanMethod) {
	Boolean scopedProxy = scopedProxyCache.get(beanMethod);
	if (scopedProxy == null) {
		AnnotationAttributes scope =
				AnnotatedElementUtils.findMergedAnnotationAttributes(beanMethod, Scope.class, false, false);
		scopedProxy = (scope != null && scope.getEnum("proxyMode") != ScopedProxyMode.NO);
		scopedProxyCache.put(beanMethod, scopedProxy);
	}
	return scopedProxy;
}
 
@Override
@Nullable
public TransactionAttribute parseTransactionAnnotation(AnnotatedElement element) {
	// 解析事务注解的属性
	AnnotationAttributes attributes = AnnotatedElementUtils.findMergedAnnotationAttributes(
			element, Transactional.class, false, false);
	if (attributes != null) {
		return parseTransactionAnnotation(attributes);
	}
	else {
		return null;
	}
}
 
源代码5 项目: spring-analysis-note   文件: MergedSqlConfig.java
/**
 * Construct a {@code MergedSqlConfig} instance by merging the configuration
 * from the supplied local (potentially method-level) {@code @SqlConfig} annotation
 * with class-level configuration discovered on the supplied {@code testClass}.
 * <p>Local configuration overrides class-level configuration.
 * <p>If the test class is not annotated with {@code @SqlConfig}, no merging
 * takes place and the local configuration is used "as is".
 */
MergedSqlConfig(SqlConfig localSqlConfig, Class<?> testClass) {
	Assert.notNull(localSqlConfig, "Local @SqlConfig must not be null");
	Assert.notNull(testClass, "testClass must not be null");

	// Get global attributes, if any.
	AnnotationAttributes attributes = AnnotatedElementUtils.findMergedAnnotationAttributes(
			testClass, SqlConfig.class.getName(), false, false);

	// Override global attributes with local attributes.
	if (attributes != null) {
		for (String key : attributes.keySet()) {
			Object value = AnnotationUtils.getValue(localSqlConfig, key);
			if (value != null) {
				// Is the value explicit (i.e., not a 'default')?
				if (!value.equals("") && value != TransactionMode.DEFAULT && value != ErrorMode.DEFAULT) {
					attributes.put(key, value);
				}
			}
		}
	}
	else {
		// Otherwise, use local attributes only.
		attributes = AnnotationUtils.getAnnotationAttributes(localSqlConfig, false, false);
	}

	this.dataSource = attributes.getString("dataSource");
	this.transactionManager = attributes.getString("transactionManager");
	this.transactionMode = getEnum(attributes, "transactionMode", TransactionMode.DEFAULT, TransactionMode.INFERRED);
	this.encoding = attributes.getString("encoding");
	this.separator = getString(attributes, "separator", ScriptUtils.DEFAULT_STATEMENT_SEPARATOR);
	this.commentPrefix = getString(attributes, "commentPrefix", ScriptUtils.DEFAULT_COMMENT_PREFIX);
	this.blockCommentStartDelimiter = getString(attributes, "blockCommentStartDelimiter",
			ScriptUtils.DEFAULT_BLOCK_COMMENT_START_DELIMITER);
	this.blockCommentEndDelimiter = getString(attributes, "blockCommentEndDelimiter",
			ScriptUtils.DEFAULT_BLOCK_COMMENT_END_DELIMITER);
	this.errorMode = getEnum(attributes, "errorMode", ErrorMode.DEFAULT, ErrorMode.FAIL_ON_ERROR);
}
 
源代码6 项目: spring-analysis-note   文件: BootstrapUtils.java
private static Class<?> resolveDefaultTestContextBootstrapper(Class<?> testClass) throws Exception {
	ClassLoader classLoader = BootstrapUtils.class.getClassLoader();
	AnnotationAttributes attributes = AnnotatedElementUtils.findMergedAnnotationAttributes(testClass,
		WEB_APP_CONFIGURATION_ANNOTATION_CLASS_NAME, false, false);
	if (attributes != null) {
		return ClassUtils.forName(DEFAULT_WEB_TEST_CONTEXT_BOOTSTRAPPER_CLASS_NAME, classLoader);
	}
	return ClassUtils.forName(DEFAULT_TEST_CONTEXT_BOOTSTRAPPER_CLASS_NAME, classLoader);
}
 
public AnnotationDescriptor(Class<?> rootDeclaringClass, Class<?> declaringClass,
		@Nullable Annotation composedAnnotation, T annotation) {

	Assert.notNull(rootDeclaringClass, "'rootDeclaringClass' must not be null");
	Assert.notNull(annotation, "Annotation must not be null");
	this.rootDeclaringClass = rootDeclaringClass;
	this.declaringClass = declaringClass;
	this.composedAnnotation = composedAnnotation;
	this.annotation = annotation;
	AnnotationAttributes attributes = AnnotatedElementUtils.findMergedAnnotationAttributes(
			rootDeclaringClass, annotation.annotationType().getName(), false, false);
	Assert.state(attributes != null, "No annotation attributes");
	this.annotationAttributes = attributes;
}
 
public static boolean isScopedProxy(Method beanMethod) {
	Boolean scopedProxy = scopedProxyCache.get(beanMethod);
	if (scopedProxy == null) {
		AnnotationAttributes scope =
				AnnotatedElementUtils.findMergedAnnotationAttributes(beanMethod, Scope.class, false, false);
		scopedProxy = (scope != null && scope.getEnum("proxyMode") != ScopedProxyMode.NO);
		scopedProxyCache.put(beanMethod, scopedProxy);
	}
	return scopedProxy;
}
 
@Override
@Nullable
public TransactionAttribute parseTransactionAnnotation(AnnotatedElement element) {
	AnnotationAttributes attributes = AnnotatedElementUtils.findMergedAnnotationAttributes(
			element, Transactional.class, false, false);
	if (attributes != null) {
		return parseTransactionAnnotation(attributes);
	}
	else {
		return null;
	}
}
 
源代码10 项目: java-technology-stack   文件: MergedSqlConfig.java
/**
 * Construct a {@code MergedSqlConfig} instance by merging the configuration
 * from the supplied local (potentially method-level) {@code @SqlConfig} annotation
 * with class-level configuration discovered on the supplied {@code testClass}.
 * <p>Local configuration overrides class-level configuration.
 * <p>If the test class is not annotated with {@code @SqlConfig}, no merging
 * takes place and the local configuration is used "as is".
 */
MergedSqlConfig(SqlConfig localSqlConfig, Class<?> testClass) {
	Assert.notNull(localSqlConfig, "Local @SqlConfig must not be null");
	Assert.notNull(testClass, "testClass must not be null");

	// Get global attributes, if any.
	AnnotationAttributes attributes = AnnotatedElementUtils.findMergedAnnotationAttributes(
			testClass, SqlConfig.class.getName(), false, false);

	// Override global attributes with local attributes.
	if (attributes != null) {
		for (String key : attributes.keySet()) {
			Object value = AnnotationUtils.getValue(localSqlConfig, key);
			if (value != null) {
				// Is the value explicit (i.e., not a 'default')?
				if (!value.equals("") && value != TransactionMode.DEFAULT && value != ErrorMode.DEFAULT) {
					attributes.put(key, value);
				}
			}
		}
	}
	else {
		// Otherwise, use local attributes only.
		attributes = AnnotationUtils.getAnnotationAttributes(localSqlConfig, false, false);
	}

	this.dataSource = attributes.getString("dataSource");
	this.transactionManager = attributes.getString("transactionManager");
	this.transactionMode = getEnum(attributes, "transactionMode", TransactionMode.DEFAULT, TransactionMode.INFERRED);
	this.encoding = attributes.getString("encoding");
	this.separator = getString(attributes, "separator", ScriptUtils.DEFAULT_STATEMENT_SEPARATOR);
	this.commentPrefix = getString(attributes, "commentPrefix", ScriptUtils.DEFAULT_COMMENT_PREFIX);
	this.blockCommentStartDelimiter = getString(attributes, "blockCommentStartDelimiter",
			ScriptUtils.DEFAULT_BLOCK_COMMENT_START_DELIMITER);
	this.blockCommentEndDelimiter = getString(attributes, "blockCommentEndDelimiter",
			ScriptUtils.DEFAULT_BLOCK_COMMENT_END_DELIMITER);
	this.errorMode = getEnum(attributes, "errorMode", ErrorMode.DEFAULT, ErrorMode.FAIL_ON_ERROR);
}
 
源代码11 项目: java-technology-stack   文件: BootstrapUtils.java
private static Class<?> resolveDefaultTestContextBootstrapper(Class<?> testClass) throws Exception {
	ClassLoader classLoader = BootstrapUtils.class.getClassLoader();
	AnnotationAttributes attributes = AnnotatedElementUtils.findMergedAnnotationAttributes(testClass,
		WEB_APP_CONFIGURATION_ANNOTATION_CLASS_NAME, false, false);
	if (attributes != null) {
		return ClassUtils.forName(DEFAULT_WEB_TEST_CONTEXT_BOOTSTRAPPER_CLASS_NAME, classLoader);
	}
	return ClassUtils.forName(DEFAULT_TEST_CONTEXT_BOOTSTRAPPER_CLASS_NAME, classLoader);
}
 
public AnnotationDescriptor(Class<?> rootDeclaringClass, Class<?> declaringClass,
		@Nullable Annotation composedAnnotation, T annotation) {

	Assert.notNull(rootDeclaringClass, "'rootDeclaringClass' must not be null");
	Assert.notNull(annotation, "Annotation must not be null");
	this.rootDeclaringClass = rootDeclaringClass;
	this.declaringClass = declaringClass;
	this.composedAnnotation = composedAnnotation;
	this.annotation = annotation;
	AnnotationAttributes attributes = AnnotatedElementUtils.findMergedAnnotationAttributes(
			rootDeclaringClass, annotation.annotationType().getName(), false, false);
	Assert.state(attributes != null, "No annotation attributes");
	this.annotationAttributes = attributes;
}
 
源代码13 项目: spring4-understanding   文件: MergedSqlConfig.java
/**
 * Construct a {@code MergedSqlConfig} instance by merging the configuration
 * from the supplied local (potentially method-level) {@code @SqlConfig} annotation
 * with class-level configuration discovered on the supplied {@code testClass}.
 * <p>Local configuration overrides class-level configuration.
 * <p>If the test class is not annotated with {@code @SqlConfig}, no merging
 * takes place and the local configuration is used "as is".
 */
MergedSqlConfig(SqlConfig localSqlConfig, Class<?> testClass) {
	Assert.notNull(localSqlConfig, "Local @SqlConfig must not be null");
	Assert.notNull(testClass, "testClass must not be null");

	// Get global attributes, if any.
	AnnotationAttributes attributes = AnnotatedElementUtils.findMergedAnnotationAttributes(testClass,
		SqlConfig.class.getName(), false, false);

	// Override global attributes with local attributes.
	if (attributes != null) {
		for (String key : attributes.keySet()) {
			Object value = AnnotationUtils.getValue(localSqlConfig, key);
			if (value != null) {
				// Is the value explicit (i.e., not a 'default')?
				if (!value.equals("") && (value != TransactionMode.DEFAULT) && (value != ErrorMode.DEFAULT)) {
					attributes.put(key, value);
				}
			}
		}
	}
	else {
		// Otherwise, use local attributes only.
		attributes = AnnotationUtils.getAnnotationAttributes(localSqlConfig, false, false);
	}

	this.dataSource = attributes.getString("dataSource");
	this.transactionManager = attributes.getString("transactionManager");
	this.transactionMode = getEnum(attributes, "transactionMode", TransactionMode.DEFAULT, TransactionMode.INFERRED);
	this.encoding = attributes.getString("encoding");
	this.separator = getString(attributes, "separator", ScriptUtils.DEFAULT_STATEMENT_SEPARATOR);
	this.commentPrefix = getString(attributes, "commentPrefix", ScriptUtils.DEFAULT_COMMENT_PREFIX);
	this.blockCommentStartDelimiter = getString(attributes, "blockCommentStartDelimiter",
		ScriptUtils.DEFAULT_BLOCK_COMMENT_START_DELIMITER);
	this.blockCommentEndDelimiter = getString(attributes, "blockCommentEndDelimiter",
		ScriptUtils.DEFAULT_BLOCK_COMMENT_END_DELIMITER);
	this.errorMode = getEnum(attributes, "errorMode", ErrorMode.DEFAULT, ErrorMode.FAIL_ON_ERROR);
}
 
public AnnotationDescriptor(Class<?> rootDeclaringClass, Class<?> declaringClass,
		Annotation composedAnnotation, T annotation) {
	Assert.notNull(rootDeclaringClass, "rootDeclaringClass must not be null");
	Assert.notNull(annotation, "annotation must not be null");
	this.rootDeclaringClass = rootDeclaringClass;
	this.declaringClass = declaringClass;
	this.composedAnnotation = composedAnnotation;
	this.annotation = annotation;
	this.annotationAttributes = AnnotatedElementUtils.findMergedAnnotationAttributes(rootDeclaringClass,
		annotation.annotationType().getName(), false, false);
}
 
private static boolean isAnnotatedWithWebAppConfiguration(Class<?> testClass) {
	return (AnnotatedElementUtils.findMergedAnnotationAttributes(testClass,
			WEB_APP_CONFIGURATION_ANNOTATION_CLASS_NAME, false, false) != null);
}
 
private static boolean isAnnotatedWithWebAppConfiguration(Class<?> testClass) {
	return (AnnotatedElementUtils.findMergedAnnotationAttributes(testClass,
			WEB_APP_CONFIGURATION_ANNOTATION_CLASS_NAME, false, false) != null);
}