Polishing around @Enable processing and @EnableTransactionManagement

Issue: SPR-11251
This commit is contained in:
Juergen Hoeller
2013-12-23 23:09:00 +01:00
parent ecee20f1fe
commit 5dddb492b8
6 changed files with 28 additions and 44 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,8 +23,6 @@ import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.Assert;
import static org.springframework.context.annotation.MetadataUtils.*;
/**
* Convenient base class for {@link ImportSelector} implementations that select imports
* based on an {@link AdviceMode} value from an annotation (such as the {@code @Enable*}
@@ -40,6 +38,7 @@ public abstract class AdviceModeImportSelector<A extends Annotation> implements
public static final String DEFAULT_ADVICE_MODE_ATTRIBUTE_NAME = "mode";
/**
* The name of the {@link AdviceMode} attribute for the annotation specified by the
* generic type {@code A}. The default is {@value #DEFAULT_ADVICE_MODE_ATTRIBUTE_NAME},
@@ -50,47 +49,37 @@ public abstract class AdviceModeImportSelector<A extends Annotation> implements
}
/**
* {@inheritDoc}
*
* <p>This implementation resolves the type of annotation from generic metadata and
* This implementation resolves the type of annotation from generic metadata and
* validates that (a) the annotation is in fact present on the importing
* {@code @Configuration} class and (b) that the given annotation has an
* {@linkplain #getAdviceModeAttributeName() advice mode attribute} of type
* {@link AdviceMode}.
*
* <p>The {@link #selectImports(AdviceMode)} method is then invoked, allowing the
* concrete implementation to choose imports in a safe and convenient fashion.
*
* @throws IllegalArgumentException if expected annotation {@code A} is not present
* on the importing {@code @Configuration} class or if {@link #selectImports(AdviceMode)}
* returns {@code null}
*/
public final String[] selectImports(AnnotationMetadata importingClassMetadata) {
Class<?> annoType = GenericTypeResolver.resolveTypeArgument(this.getClass(), AdviceModeImportSelector.class);
AnnotationAttributes attributes = attributesFor(importingClassMetadata, annoType);
Class<?> annoType = GenericTypeResolver.resolveTypeArgument(getClass(), AdviceModeImportSelector.class);
AnnotationAttributes attributes = MetadataUtils.attributesFor(importingClassMetadata, annoType);
Assert.notNull(attributes, String.format(
"@%s is not present on importing class '%s' as expected",
annoType.getSimpleName(), importingClassMetadata.getClassName()));
AdviceMode adviceMode = attributes.getEnum(this.getAdviceModeAttributeName());
String[] imports = selectImports(adviceMode);
Assert.notNull(imports, String.format("Unknown AdviceMode: '%s'", adviceMode));
return imports;
}
/**
* Determine which classes should be imported based on the given {@code AdviceMode}.
*
* <p>Returning {@code null} from this method indicates that the {@code AdviceMode} could
* not be handled or was unknown and that an {@code IllegalArgumentException} should
* be thrown.
*
* @param adviceMode the value of the {@linkplain #getAdviceModeAttributeName()
* advice mode attribute} for the annotation specified via generics.
*
* @return array containing classes to import; empty array if none, {@code null} if
* the given {@code AdviceMode} is unknown.
*/

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,12 +16,11 @@
package org.springframework.context.annotation;
import static org.springframework.context.annotation.MetadataUtils.attributesFor;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.config.AopConfigUtils;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.core.annotation.AnnotationAttributes;
@@ -33,8 +32,8 @@ import org.springframework.core.type.AnnotationMetadata;
* {@code proxyTargetClass} attributes set to the correct values.
*
* @author Chris Beams
* @see EnableAspectJAutoProxy
* @since 3.1
* @see EnableAspectJAutoProxy
*/
public class AutoProxyRegistrar implements ImportBeanDefinitionRegistrar {
@@ -47,7 +46,6 @@ public class AutoProxyRegistrar implements ImportBeanDefinitionRegistrar {
* attributes. If {@code mode} is set to {@code PROXY}, the APC is registered; if
* {@code proxyTargetClass} is set to {@code true}, then the APC is forced to use
* subclass (CGLIB) proxying.
*
* <p>Several {@code @Enable*} annotations expose both {@code mode} and
* {@code proxyTargetClass} attributes. It is important to note that most of these
* capabilities end up sharing a {@linkplain AopConfigUtils#AUTO_PROXY_CREATOR_BEAN_NAME
@@ -60,16 +58,15 @@ public class AutoProxyRegistrar implements ImportBeanDefinitionRegistrar {
boolean candidateFound = false;
Set<String> annoTypes = importingClassMetadata.getAnnotationTypes();
for (String annoType : annoTypes) {
AnnotationAttributes candidate = attributesFor(importingClassMetadata, annoType);
AnnotationAttributes candidate = MetadataUtils.attributesFor(importingClassMetadata, annoType);
Object mode = candidate.get("mode");
Object proxyTargetClass = candidate.get("proxyTargetClass");
if (mode != null && proxyTargetClass != null
&& mode.getClass().equals(AdviceMode.class)
&& proxyTargetClass.getClass().equals(Boolean.class)) {
if (mode != null && proxyTargetClass != null && mode.getClass().equals(AdviceMode.class) &&
proxyTargetClass.getClass().equals(Boolean.class)) {
candidateFound = true;
if (mode == AdviceMode.PROXY) {
AopConfigUtils.registerAutoProxyCreatorIfNecessary(registry);
if ((Boolean)proxyTargetClass) {
if ((Boolean) proxyTargetClass) {
AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry);
return;
}
@@ -88,4 +85,5 @@ public class AutoProxyRegistrar implements ImportBeanDefinitionRegistrar {
"altogether.", name, name, name));
}
}
}

View File

@@ -51,8 +51,6 @@ import org.springframework.core.type.MethodMetadata;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.util.StringUtils;
import static org.springframework.context.annotation.MetadataUtils.*;
/**
* Reads a given fully-populated set of ConfigurationClass instances, registering bean
* definitions with the given {@link BeanDefinitionRegistry} based on its contents.
@@ -173,13 +171,13 @@ class ConfigurationClassBeanDefinitionReader {
beanDef.setAttribute(RequiredAnnotationBeanPostProcessor.SKIP_REQUIRED_CHECK_ATTRIBUTE, Boolean.TRUE);
// consider role
AnnotationAttributes role = attributesFor(metadata, Role.class);
AnnotationAttributes role = MetadataUtils.attributesFor(metadata, Role.class);
if (role != null) {
beanDef.setRole(role.<Integer>getNumber("value"));
}
// consider name and any aliases
AnnotationAttributes bean = attributesFor(metadata, Bean.class);
AnnotationAttributes bean = MetadataUtils.attributesFor(metadata, Bean.class);
List<String> names = new ArrayList<String>(Arrays.asList(bean.getStringArray("name")));
String beanName = (names.size() > 0 ? names.remove(0) : beanMethod.getMetadata().getMethodName());
for (String alias : names) {
@@ -216,16 +214,16 @@ class ConfigurationClassBeanDefinitionReader {
// is this bean to be instantiated lazily?
if (metadata.isAnnotated(Lazy.class.getName())) {
AnnotationAttributes lazy = attributesFor(metadata, Lazy.class);
AnnotationAttributes lazy = MetadataUtils.attributesFor(metadata, Lazy.class);
beanDef.setLazyInit(lazy.getBoolean("value"));
}
else if (configClass.getMetadata().isAnnotated(Lazy.class.getName())){
AnnotationAttributes lazy = attributesFor(configClass.getMetadata(), Lazy.class);
AnnotationAttributes lazy = MetadataUtils.attributesFor(configClass.getMetadata(), Lazy.class);
beanDef.setLazyInit(lazy.getBoolean("value"));
}
if (metadata.isAnnotated(DependsOn.class.getName())) {
AnnotationAttributes dependsOn = attributesFor(metadata, DependsOn.class);
AnnotationAttributes dependsOn = MetadataUtils.attributesFor(metadata, DependsOn.class);
String[] otherBeans = dependsOn.getStringArray("value");
if (otherBeans.length > 0) {
beanDef.setDependsOn(otherBeans);
@@ -249,7 +247,7 @@ class ConfigurationClassBeanDefinitionReader {
// Consider scoping
ScopedProxyMode proxyMode = ScopedProxyMode.NO;
AnnotationAttributes scope = attributesFor(metadata, Scope.class);
AnnotationAttributes scope = MetadataUtils.attributesFor(metadata, Scope.class);
if (scope != null) {
beanDef.setScope(scope.getString("value"));
proxyMode = scope.getEnum("proxyMode");
@@ -272,7 +270,7 @@ class ConfigurationClassBeanDefinitionReader {
configClass.getMetadata().getClassName(), beanName));
}
registry.registerBeanDefinition(beanName, beanDefToRegister);
this.registry.registerBeanDefinition(beanName, beanDefToRegister);
}
@@ -297,7 +295,8 @@ class ConfigurationClassBeanDefinitionReader {
readerInstanceCache.put(readerClass, readerInstance);
}
catch (Exception ex) {
throw new IllegalStateException("Could not instantiate BeanDefinitionReader class [" + readerClass.getName() + "]");
throw new IllegalStateException(
"Could not instantiate BeanDefinitionReader class [" + readerClass.getName() + "]");
}
}
BeanDefinitionReader reader = readerInstanceCache.get(readerClass);