Refactor to consistent use of AnnotationAttributes

Uses of AnnotationMetadata#getAnnotationAttributes throughout the
framework have been updated to use the new AnnotationAttributes API in
order to take advantage of the more concise, expressive and type-safe
methods there.

All changes are binary compatible to the 3.1.0 public API, save
the exception below.

A minor binary compatibility issue has been introduced in
AbstractCachingConfiguration, AbstractAsyncConfiguration and
AbstractTransactionManagementConfiguration when updating their
protected Map<String, Object> fields representing annotation attributes
to use the new AnnotationAttributes API. This is a negligible breakage,
however, as the likelilhood of users subclassing these types is very
low, the classes have only been in existence for a short time (further
reducing the likelihood), and it is a source-compatible change given
that AnnotationAttributes is assignable to Map<String, Object>.
This commit is contained in:
Chris Beams 2012-02-07 19:45:46 +01:00
parent d9f7fdd120
commit bf541db5b0
20 changed files with 151 additions and 169 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2011 the original author or authors. * Copyright 2002-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,7 +17,6 @@
package org.springframework.cache.annotation; package org.springframework.cache.annotation;
import java.util.Collection; import java.util.Collection;
import java.util.Map;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
@ -26,6 +25,7 @@ import org.springframework.cache.CacheManager;
import org.springframework.cache.interceptor.KeyGenerator; import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportAware; import org.springframework.context.annotation.ImportAware;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata; import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
@ -41,8 +41,7 @@ import org.springframework.util.CollectionUtils;
@Configuration @Configuration
public abstract class AbstractCachingConfiguration implements ImportAware { public abstract class AbstractCachingConfiguration implements ImportAware {
/** Parsed annotation metadata for {@code @EnableCaching} on the importing class. */ protected AnnotationAttributes enableCaching;
protected Map<String, Object> enableCaching;
protected CacheManager cacheManager; protected CacheManager cacheManager;
protected KeyGenerator keyGenerator; protected KeyGenerator keyGenerator;
@ -52,8 +51,8 @@ public abstract class AbstractCachingConfiguration implements ImportAware {
private Collection<CachingConfigurer> cachingConfigurers; private Collection<CachingConfigurer> cachingConfigurers;
public void setImportMetadata(AnnotationMetadata importMetadata) { public void setImportMetadata(AnnotationMetadata importMetadata) {
this.enableCaching = importMetadata.getAnnotationAttributes( this.enableCaching = AnnotationAttributes.fromMap(
EnableCaching.class.getName(), false); importMetadata.getAnnotationAttributes(EnableCaching.class.getName(), false));
Assert.notNull(this.enableCaching, Assert.notNull(this.enableCaching,
"@EnableCaching is not present on importing class " + "@EnableCaching is not present on importing class " +
importMetadata.getClassName()); importMetadata.getClassName());

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2011 the original author or authors. * Copyright 2002-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -44,7 +44,7 @@ public class ProxyCachingConfiguration extends AbstractCachingConfiguration {
new BeanFactoryCacheOperationSourceAdvisor(); new BeanFactoryCacheOperationSourceAdvisor();
advisor.setCacheOperationSource(cacheOperationSource()); advisor.setCacheOperationSource(cacheOperationSource());
advisor.setAdvice(cacheInterceptor()); advisor.setAdvice(cacheInterceptor());
advisor.setOrder(((Integer)this.enableCaching.get("order"))); advisor.setOrder(this.enableCaching.getInt("order"));
return advisor; return advisor;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2011 the original author or authors. * Copyright 2002-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,10 +16,12 @@
package org.springframework.context.annotation; package org.springframework.context.annotation;
import static org.springframework.context.annotation.MetadataUtils.attributesFor;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.util.Map;
import org.springframework.core.GenericTypeResolver; import org.springframework.core.GenericTypeResolver;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata; import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@ -64,26 +66,17 @@ public abstract class AdviceModeImportSelector<A extends Annotation> implements
* returns {@code null} * returns {@code null}
*/ */
public final String[] selectImports(AnnotationMetadata importingClassMetadata) { public final String[] selectImports(AnnotationMetadata importingClassMetadata) {
Class<?> annoType = GenericTypeResolver.resolveTypeArgument(getClass(), AdviceModeImportSelector.class); Class<?> annoType = GenericTypeResolver.resolveTypeArgument(this.getClass(), AdviceModeImportSelector.class);
Map<String, Object> attributes = importingClassMetadata.getAnnotationAttributes(annoType.getName()); AnnotationAttributes attributes = attributesFor(importingClassMetadata, annoType);
Assert.notNull(attributes, String.format( Assert.notNull(attributes, String.format(
"@%s is not present on importing class '%s' as expected", "@%s is not present on importing class '%s' as expected",
annoType.getSimpleName(), importingClassMetadata.getClassName())); annoType.getSimpleName(), importingClassMetadata.getClassName()));
String modeAttrName = getAdviceModeAttributeName(); AdviceMode adviceMode =
Assert.hasText(modeAttrName); attributes.getEnum(this.getAdviceModeAttributeName(), AdviceMode.class);
Object adviceMode = attributes.get(modeAttrName); String[] imports = selectImports(adviceMode);
Assert.notNull(adviceMode, String.format(
"Advice mode attribute @%s#%s() does not exist",
annoType.getSimpleName(), modeAttrName));
Assert.isInstanceOf(AdviceMode.class, adviceMode, String.format(
"Incorrect type for advice mode attribute '@%s#%s()': ",
annoType.getSimpleName(), modeAttrName));
String[] imports = selectImports((AdviceMode) adviceMode);
Assert.notNull(imports, String.format("Unknown AdviceMode: '%s'", adviceMode)); Assert.notNull(imports, String.format("Unknown AdviceMode: '%s'", adviceMode));
return imports; return imports;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2011 the original author or authors. * Copyright 2002-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -24,6 +24,7 @@ import org.springframework.beans.factory.support.AutowireCandidateQualifier;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils; import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator; import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.core.env.EnvironmentCapable; import org.springframework.core.env.EnvironmentCapable;
import org.springframework.core.env.StandardEnvironment; import org.springframework.core.env.StandardEnvironment;
@ -136,8 +137,9 @@ public class AnnotatedBeanDefinitionReader {
AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(annotatedClass); AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(annotatedClass);
AnnotationMetadata metadata = abd.getMetadata(); AnnotationMetadata metadata = abd.getMetadata();
if (ProfileHelper.isProfileAnnotationPresent(metadata)) { if (metadata.isAnnotated(Profile.class.getName())) {
if (!this.environment.acceptsProfiles(ProfileHelper.getCandidateProfiles(metadata))) { AnnotationAttributes profile = MetadataUtils.attributesFor(metadata, Profile.class);
if (!this.environment.acceptsProfiles(profile.getStringArray("value"))) {
return; return;
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2009 the original author or authors. * Copyright 2002-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -24,6 +24,7 @@ import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator; import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata; import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -85,7 +86,7 @@ public class AnnotationBeanNameGenerator implements BeanNameGenerator {
Set<String> types = amd.getAnnotationTypes(); Set<String> types = amd.getAnnotationTypes();
String beanName = null; String beanName = null;
for (String type : types) { for (String type : types) {
Map<String, Object> attributes = amd.getAnnotationAttributes(type); AnnotationAttributes attributes = MetadataUtils.attributesFor(amd, type);
if (isStereotypeWithNameValue(type, amd.getMetaAnnotationTypes(type), attributes)) { if (isStereotypeWithNameValue(type, amd.getMetaAnnotationTypes(type), attributes)) {
String value = (String) attributes.get("value"); String value = (String) attributes.get("value");
if (StringUtils.hasLength(value)) { if (StringUtils.hasLength(value)) {

View File

@ -16,6 +16,8 @@
package org.springframework.context.annotation; package org.springframework.context.annotation;
import static org.springframework.context.annotation.MetadataUtils.attributesFor;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.Set; import java.util.Set;
@ -27,6 +29,7 @@ import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
/** /**
@ -217,21 +220,20 @@ public class AnnotationConfigUtils {
} }
static void processCommonDefinitionAnnotations(AnnotatedBeanDefinition abd) { static void processCommonDefinitionAnnotations(AnnotatedBeanDefinition abd) {
if (abd.getMetadata().isAnnotated(Primary.class.getName())) { AnnotationMetadata metadata = abd.getMetadata();
if (metadata.isAnnotated(Primary.class.getName())) {
abd.setPrimary(true); abd.setPrimary(true);
} }
if (abd.getMetadata().isAnnotated(Lazy.class.getName())) { if (metadata.isAnnotated(Lazy.class.getName())) {
Boolean value = (Boolean) abd.getMetadata().getAnnotationAttributes(Lazy.class.getName()).get("value"); abd.setLazyInit(attributesFor(metadata, Lazy.class).getBoolean("value"));
abd.setLazyInit(value);
} }
if (abd.getMetadata().isAnnotated(DependsOn.class.getName())) { if (metadata.isAnnotated(DependsOn.class.getName())) {
String[] value = (String[]) abd.getMetadata().getAnnotationAttributes(DependsOn.class.getName()).get("value"); abd.setDependsOn(attributesFor(metadata, DependsOn.class).getStringArray("value"));
abd.setDependsOn(value);
} }
if (abd instanceof AbstractBeanDefinition) { if (abd instanceof AbstractBeanDefinition) {
if (abd.getMetadata().isAnnotated(Role.class.getName())) { if (metadata.isAnnotated(Role.class.getName())) {
int value = (Integer) abd.getMetadata().getAnnotationAttributes(Role.class.getName()).get("value"); ((AbstractBeanDefinition)abd).setRole(
((AbstractBeanDefinition)abd).setRole(value); attributesFor(metadata, Role.class).getInt("value"));
} }
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2011 the original author or authors. * Copyright 2002-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,11 +16,13 @@
package org.springframework.context.annotation; package org.springframework.context.annotation;
import static org.springframework.context.annotation.MetadataUtils.attributesFor;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.util.Map;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition; import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.util.Assert; import org.springframework.util.Assert;
/** /**
@ -75,11 +77,11 @@ public class AnnotationScopeMetadataResolver implements ScopeMetadataResolver {
ScopeMetadata metadata = new ScopeMetadata(); ScopeMetadata metadata = new ScopeMetadata();
if (definition instanceof AnnotatedBeanDefinition) { if (definition instanceof AnnotatedBeanDefinition) {
AnnotatedBeanDefinition annDef = (AnnotatedBeanDefinition) definition; AnnotatedBeanDefinition annDef = (AnnotatedBeanDefinition) definition;
Map<String, Object> attributes = AnnotationAttributes attributes =
annDef.getMetadata().getAnnotationAttributes(this.scopeAnnotationType.getName()); attributesFor(annDef.getMetadata(), this.scopeAnnotationType);
if (attributes != null) { if (attributes != null) {
metadata.setScopeName((String) attributes.get("value")); metadata.setScopeName(attributes.getString("value"));
ScopedProxyMode proxyMode = (ScopedProxyMode) attributes.get("proxyMode"); ScopedProxyMode proxyMode = attributes.getEnum("proxyMode", ScopedProxyMode.class);
if (proxyMode == null || proxyMode == ScopedProxyMode.DEFAULT) { if (proxyMode == null || proxyMode == ScopedProxyMode.DEFAULT) {
proxyMode = this.defaultProxyMode; proxyMode = this.defaultProxyMode;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2011 the original author or authors. * Copyright 2002-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,10 +16,11 @@
package org.springframework.context.annotation; package org.springframework.context.annotation;
import java.util.Map; import static org.springframework.context.annotation.MetadataUtils.attributesFor;
import org.springframework.aop.config.AopConfigUtils; import org.springframework.aop.config.AopConfigUtils;
import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata; import org.springframework.core.type.AnnotationMetadata;
/** /**
@ -41,12 +42,11 @@ class AspectJAutoProxyRegistrar implements ImportBeanDefinitionRegistrar {
public void registerBeanDefinitions( public void registerBeanDefinitions(
AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
Map<String, Object> enableAJAutoProxy =
importingClassMetadata.getAnnotationAttributes(EnableAspectJAutoProxy.class.getName());
AopConfigUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(registry); AopConfigUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(registry);
if ((Boolean)enableAJAutoProxy.get("proxyTargetClass")) { AnnotationAttributes enableAJAutoProxy =
attributesFor(importingClassMetadata, EnableAspectJAutoProxy.class);
if (enableAJAutoProxy.getBoolean("proxyTargetClass")) {
AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry); AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry);
} }
} }

View File

@ -16,6 +16,8 @@
package org.springframework.context.annotation; package org.springframework.context.annotation;
import static org.springframework.context.annotation.MetadataUtils.attributesFor;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -23,6 +25,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.aop.config.AopConfigUtils; import org.springframework.aop.config.AopConfigUtils;
import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata; import org.springframework.core.type.AnnotationMetadata;
/** /**
@ -58,7 +61,7 @@ public class AutoProxyRegistrar implements ImportBeanDefinitionRegistrar {
boolean candidateFound = false; boolean candidateFound = false;
Set<String> annoTypes = importingClassMetadata.getAnnotationTypes(); Set<String> annoTypes = importingClassMetadata.getAnnotationTypes();
for (String annoType : annoTypes) { for (String annoType : annoTypes) {
Map<String, Object> candidate = importingClassMetadata.getAnnotationAttributes(annoType); AnnotationAttributes candidate = attributesFor(importingClassMetadata, annoType);
Object mode = candidate.get("mode"); Object mode = candidate.get("mode");
Object proxyTargetClass = candidate.get("proxyTargetClass"); Object proxyTargetClass = candidate.get("proxyTargetClass");
if (mode != null && proxyTargetClass != null if (mode != null && proxyTargetClass != null

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2009 the original author or authors. * Copyright 2002-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -29,6 +29,7 @@ import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition; import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.ResourceLoaderAware; import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.env.StandardEnvironment; import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.core.env.EnvironmentCapable; import org.springframework.core.env.EnvironmentCapable;
@ -302,10 +303,11 @@ public class ClassPathScanningCandidateComponentProvider implements EnvironmentC
for (TypeFilter tf : this.includeFilters) { for (TypeFilter tf : this.includeFilters) {
if (tf.match(metadataReader, this.metadataReaderFactory)) { if (tf.match(metadataReader, this.metadataReaderFactory)) {
AnnotationMetadata metadata = metadataReader.getAnnotationMetadata(); AnnotationMetadata metadata = metadataReader.getAnnotationMetadata();
if (!ProfileHelper.isProfileAnnotationPresent(metadata)) { if (!metadata.isAnnotated(Profile.class.getName())) {
return true; return true;
} }
return this.environment.acceptsProfiles(ProfileHelper.getCandidateProfiles(metadata)); AnnotationAttributes profile = MetadataUtils.attributesFor(metadata, Profile.class);
return this.environment.acceptsProfiles(profile.getStringArray("value"));
} }
} }
return false; return false;

View File

@ -25,6 +25,7 @@ import java.util.Set;
import org.springframework.beans.factory.parsing.Location; import org.springframework.beans.factory.parsing.Location;
import org.springframework.beans.factory.parsing.Problem; import org.springframework.beans.factory.parsing.Problem;
import org.springframework.beans.factory.parsing.ProblemReporter; import org.springframework.beans.factory.parsing.ProblemReporter;
import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.core.io.DescriptiveResource; import org.springframework.core.io.DescriptiveResource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.core.type.AnnotationMetadata; import org.springframework.core.type.AnnotationMetadata;
@ -50,7 +51,8 @@ final class ConfigurationClass {
private final Resource resource; private final Resource resource;
private final Map<String, Class<?>> importedResources = new LinkedHashMap<String, Class<?>>(); private final Map<String, Class<? extends BeanDefinitionReader>> importedResources =
new LinkedHashMap<String, Class<? extends BeanDefinitionReader>>();
private final Set<BeanMethod> beanMethods = new LinkedHashSet<BeanMethod>(); private final Set<BeanMethod> beanMethods = new LinkedHashSet<BeanMethod>();
@ -154,11 +156,12 @@ final class ConfigurationClass {
return this.beanMethods; return this.beanMethods;
} }
public void addImportedResource(String importedResource, Class<?> readerClass) { public void addImportedResource(
String importedResource, Class<? extends BeanDefinitionReader> readerClass) {
this.importedResources.put(importedResource, readerClass); this.importedResources.put(importedResource, readerClass);
} }
public Map<String, Class<?>> getImportedResources() { public Map<String, Class<? extends BeanDefinitionReader>> getImportedResources() {
return this.importedResources; return this.importedResources;
} }

View File

@ -16,6 +16,8 @@
package org.springframework.context.annotation; package org.springframework.context.annotation;
import static org.springframework.context.annotation.MetadataUtils.attributesFor;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
@ -43,6 +45,7 @@ import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition; import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotationMetadata; import org.springframework.core.type.AnnotationMetadata;
@ -187,15 +190,14 @@ class ConfigurationClassBeanDefinitionReader {
beanDef.setAttribute(RequiredAnnotationBeanPostProcessor.SKIP_REQUIRED_CHECK_ATTRIBUTE, Boolean.TRUE); beanDef.setAttribute(RequiredAnnotationBeanPostProcessor.SKIP_REQUIRED_CHECK_ATTRIBUTE, Boolean.TRUE);
// consider role // consider role
Map<String, Object> roleAttributes = metadata.getAnnotationAttributes(Role.class.getName()); AnnotationAttributes role = attributesFor(metadata, Role.class);
if (roleAttributes != null) { if (role != null) {
int role = (Integer) roleAttributes.get("value"); beanDef.setRole(role.getInt("value"));
beanDef.setRole(role);
} }
// consider name and any aliases // consider name and any aliases
Map<String, Object> beanAttributes = metadata.getAnnotationAttributes(Bean.class.getName()); AnnotationAttributes bean = attributesFor(metadata, Bean.class);
List<String> names = new ArrayList<String>(Arrays.asList((String[]) beanAttributes.get("name"))); List<String> names = new ArrayList<String>(Arrays.asList(bean.getStringArray("name")));
String beanName = (names.size() > 0 ? names.remove(0) : beanMethod.getMetadata().getMethodName()); String beanName = (names.size() > 0 ? names.remove(0) : beanMethod.getMetadata().getMethodName());
for (String alias : names) { for (String alias : names) {
this.registry.registerAlias(beanName, alias); this.registry.registerAlias(beanName, alias);
@ -222,40 +224,43 @@ class ConfigurationClassBeanDefinitionReader {
// is this bean to be instantiated lazily? // is this bean to be instantiated lazily?
if (metadata.isAnnotated(Lazy.class.getName())) { if (metadata.isAnnotated(Lazy.class.getName())) {
beanDef.setLazyInit((Boolean) metadata.getAnnotationAttributes(Lazy.class.getName()).get("value")); AnnotationAttributes lazy = attributesFor(metadata, Lazy.class);
beanDef.setLazyInit(lazy.getBoolean("value"));
} }
else if (configClass.getMetadata().isAnnotated(Lazy.class.getName())){ else if (configClass.getMetadata().isAnnotated(Lazy.class.getName())){
beanDef.setLazyInit((Boolean) configClass.getMetadata().getAnnotationAttributes(Lazy.class.getName()).get("value")); AnnotationAttributes lazy = attributesFor(configClass.getMetadata(), Lazy.class);
beanDef.setLazyInit(lazy.getBoolean("value"));
} }
if (metadata.isAnnotated(DependsOn.class.getName())) { if (metadata.isAnnotated(DependsOn.class.getName())) {
String[] dependsOn = (String[]) metadata.getAnnotationAttributes(DependsOn.class.getName()).get("value"); AnnotationAttributes dependsOn = attributesFor(metadata, DependsOn.class);
if (dependsOn.length > 0) { String[] otherBeans = dependsOn.getStringArray("value");
beanDef.setDependsOn(dependsOn); if (otherBeans.length > 0) {
beanDef.setDependsOn(otherBeans);
} }
} }
Autowire autowire = (Autowire) beanAttributes.get("autowire"); Autowire autowire = bean.getEnum("autowire", Autowire.class);
if (autowire.isAutowire()) { if (autowire.isAutowire()) {
beanDef.setAutowireMode(autowire.value()); beanDef.setAutowireMode(autowire.value());
} }
String initMethodName = (String) beanAttributes.get("initMethod"); String initMethodName = bean.getString("initMethod");
if (StringUtils.hasText(initMethodName)) { if (StringUtils.hasText(initMethodName)) {
beanDef.setInitMethodName(initMethodName); beanDef.setInitMethodName(initMethodName);
} }
String destroyMethodName = (String) beanAttributes.get("destroyMethod"); String destroyMethodName = bean.getString("destroyMethod");
if (StringUtils.hasText(destroyMethodName)) { if (StringUtils.hasText(destroyMethodName)) {
beanDef.setDestroyMethodName(destroyMethodName); beanDef.setDestroyMethodName(destroyMethodName);
} }
// consider scoping // consider scoping
ScopedProxyMode proxyMode = ScopedProxyMode.NO; ScopedProxyMode proxyMode = ScopedProxyMode.NO;
Map<String, Object> scopeAttributes = metadata.getAnnotationAttributes(Scope.class.getName()); AnnotationAttributes scope = attributesFor(metadata, Scope.class);
if (scopeAttributes != null) { if (scope != null) {
beanDef.setScope((String) scopeAttributes.get("value")); beanDef.setScope(scope.getString("value"));
proxyMode = (ScopedProxyMode) scopeAttributes.get("proxyMode"); proxyMode = scope.getEnum("proxyMode", ScopedProxyMode.class);
if (proxyMode == ScopedProxyMode.DEFAULT) { if (proxyMode == ScopedProxyMode.DEFAULT) {
proxyMode = ScopedProxyMode.NO; proxyMode = ScopedProxyMode.NO;
} }
@ -277,15 +282,17 @@ class ConfigurationClassBeanDefinitionReader {
} }
private void loadBeanDefinitionsFromImportedResources(Map<String, Class<?>> importedResources) { private void loadBeanDefinitionsFromImportedResources(
Map<String, Class<? extends BeanDefinitionReader>> importedResources) {
Map<Class<?>, BeanDefinitionReader> readerInstanceCache = new HashMap<Class<?>, BeanDefinitionReader>(); Map<Class<?>, BeanDefinitionReader> readerInstanceCache = new HashMap<Class<?>, BeanDefinitionReader>();
for (Map.Entry<String, Class<?>> entry : importedResources.entrySet()) { for (Map.Entry<String, Class<? extends BeanDefinitionReader>> entry : importedResources.entrySet()) {
String resource = entry.getKey(); String resource = entry.getKey();
Class<?> readerClass = entry.getValue(); Class<? extends BeanDefinitionReader> readerClass = entry.getValue();
if (!readerInstanceCache.containsKey(readerClass)) { if (!readerInstanceCache.containsKey(readerClass)) {
try { try {
// Instantiate the specified BeanDefinitionReader // Instantiate the specified BeanDefinitionReader
BeanDefinitionReader readerInstance = (BeanDefinitionReader) BeanDefinitionReader readerInstance =
readerClass.getConstructor(BeanDefinitionRegistry.class).newInstance(this.registry); readerClass.getConstructor(BeanDefinitionRegistry.class).newInstance(this.registry);
// Delegate the current ResourceLoader to it if possible // Delegate the current ResourceLoader to it if possible

View File

@ -16,6 +16,8 @@
package org.springframework.context.annotation; package org.springframework.context.annotation;
import static org.springframework.context.annotation.MetadataUtils.attributesFor;
import java.io.IOException; import java.io.IOException;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.util.ArrayList; import java.util.ArrayList;
@ -34,6 +36,7 @@ import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.parsing.Location; import org.springframework.beans.factory.parsing.Location;
import org.springframework.beans.factory.parsing.Problem; import org.springframework.beans.factory.parsing.Problem;
import org.springframework.beans.factory.parsing.ProblemReporter; import org.springframework.beans.factory.parsing.ProblemReporter;
import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
@ -128,8 +131,9 @@ class ConfigurationClassParser {
protected void processConfigurationClass(ConfigurationClass configClass) throws IOException { protected void processConfigurationClass(ConfigurationClass configClass) throws IOException {
AnnotationMetadata metadata = configClass.getMetadata(); AnnotationMetadata metadata = configClass.getMetadata();
if (this.environment != null && ProfileHelper.isProfileAnnotationPresent(metadata)) { if (this.environment != null && metadata.isAnnotated(Profile.class.getName())) {
if (!this.environment.acceptsProfiles(ProfileHelper.getCandidateProfiles(metadata))) { AnnotationAttributes profile = MetadataUtils.attributesFor(metadata, Profile.class);
if (!this.environment.acceptsProfiles(profile.getStringArray("value"))) {
return; return;
} }
} }
@ -172,11 +176,11 @@ class ConfigurationClassParser {
} }
// process any @PropertySource annotations // process any @PropertySource annotations
Map<String, Object> propertySourceAttributes = AnnotationAttributes propertySource =
metadata.getAnnotationAttributes(org.springframework.context.annotation.PropertySource.class.getName()); attributesFor(metadata, org.springframework.context.annotation.PropertySource.class);
if (propertySourceAttributes != null) { if (propertySource != null) {
String name = (String) propertySourceAttributes.get("name"); String name = propertySource.getString("name");
String[] locations = (String[]) propertySourceAttributes.get("value"); String[] locations = propertySource.getStringArray("value");
ClassLoader classLoader = this.resourceLoader.getClassLoader(); ClassLoader classLoader = this.resourceLoader.getClassLoader();
for (String location : locations) { for (String location : locations) {
location = this.environment.resolveRequiredPlaceholders(location); location = this.environment.resolveRequiredPlaceholders(location);
@ -188,34 +192,32 @@ class ConfigurationClassParser {
} }
// process any @ComponentScan annotions // process any @ComponentScan annotions
AnnotationAttributes componentScan = MetadataUtils.attributesFor(metadata, ComponentScan.class); AnnotationAttributes componentScan = attributesFor(metadata, ComponentScan.class);
if (componentScan != null) { if (componentScan != null) {
// the config class is annotated with @ComponentScan -> perform the scan immediately // the config class is annotated with @ComponentScan -> perform the scan immediately
Set<BeanDefinitionHolder> scannedBeanDefinitions = this.componentScanParser.parse(componentScan); Set<BeanDefinitionHolder> scannedBeanDefinitions = this.componentScanParser.parse(componentScan);
// check the set of scanned definitions for any further config classes and parse recursively if necessary // check the set of scanned definitions for any further config classes and parse recursively if necessary
for (BeanDefinitionHolder holder : scannedBeanDefinitions) { for (BeanDefinitionHolder holder : scannedBeanDefinitions) {
if (ConfigurationClassUtils.checkConfigurationClassCandidate(holder.getBeanDefinition(), metadataReaderFactory)) { if (ConfigurationClassUtils.checkConfigurationClassCandidate(holder.getBeanDefinition(), this.metadataReaderFactory)) {
this.parse(holder.getBeanDefinition().getBeanClassName(), holder.getBeanName()); this.parse(holder.getBeanDefinition().getBeanClassName(), holder.getBeanName());
} }
} }
} }
// process any @Import annotations // process any @Import annotations
List<Map<String, Object>> allImportAttribs = List<AnnotationAttributes> imports =
findAllAnnotationAttributes(Import.class, metadata.getClassName(), true); findAllAnnotationAttributes(Import.class, metadata.getClassName(), true);
for (Map<String, Object> importAttribs : allImportAttribs) { for (AnnotationAttributes importAnno : imports) {
processImport(configClass, (String[]) importAttribs.get("value"), true); processImport(configClass, importAnno.getStringArray("value"), true);
} }
// process any @ImportResource annotations // process any @ImportResource annotations
if (metadata.isAnnotated(ImportResource.class.getName())) { if (metadata.isAnnotated(ImportResource.class.getName())) {
String[] resources = (String[]) metadata.getAnnotationAttributes(ImportResource.class.getName()).get("value"); AnnotationAttributes importResource = attributesFor(metadata, ImportResource.class);
Class<?> readerClass = (Class<?>) metadata.getAnnotationAttributes(ImportResource.class.getName()).get("reader"); String[] resources = importResource.getStringArray("value");
if (readerClass == null) { Class<? extends BeanDefinitionReader> readerClass =
throw new IllegalStateException("No reader class associated with imported resources: " + importResource.getClass("reader", BeanDefinitionReader.class);
StringUtils.arrayToCommaDelimitedString(resources));
}
for (String resource : resources) { for (String resource : resources) {
configClass.addImportedResource(resource, readerClass); configClass.addImportedResource(resource, readerClass);
} }
@ -239,11 +241,11 @@ class ConfigurationClassParser {
* @param annotatedClassName the class to inspect * @param annotatedClassName the class to inspect
* @param classValuesAsString whether class attributes should be returned as strings * @param classValuesAsString whether class attributes should be returned as strings
*/ */
private List<Map<String, Object>> findAllAnnotationAttributes( private List<AnnotationAttributes> findAllAnnotationAttributes(
Class<? extends Annotation> targetAnnotation, String annotatedClassName, Class<? extends Annotation> targetAnnotation, String annotatedClassName,
boolean classValuesAsString) throws IOException { boolean classValuesAsString) throws IOException {
List<Map<String, Object>> allAttribs = new ArrayList<Map<String, Object>>(); List<AnnotationAttributes> allAttribs = new ArrayList<AnnotationAttributes>();
MetadataReader reader = this.metadataReaderFactory.getMetadataReader(annotatedClassName); MetadataReader reader = this.metadataReaderFactory.getMetadataReader(annotatedClassName);
AnnotationMetadata metadata = reader.getAnnotationMetadata(); AnnotationMetadata metadata = reader.getAnnotationMetadata();
@ -253,16 +255,17 @@ class ConfigurationClassParser {
if (annotationType.equals(targetAnnotationType)) { if (annotationType.equals(targetAnnotationType)) {
continue; continue;
} }
MetadataReader metaReader = this.metadataReaderFactory.getMetadataReader(annotationType); AnnotationMetadata metaAnnotations =
Map<String, Object> targetAttribs = this.metadataReaderFactory.getMetadataReader(annotationType).getAnnotationMetadata();
metaReader.getAnnotationMetadata().getAnnotationAttributes(targetAnnotationType, classValuesAsString); AnnotationAttributes targetAttribs =
AnnotationAttributes.fromMap(metaAnnotations.getAnnotationAttributes(targetAnnotationType, classValuesAsString));
if (targetAttribs != null) { if (targetAttribs != null) {
allAttribs.add(targetAttribs); allAttribs.add(targetAttribs);
} }
} }
Map<String, Object> localAttribs = AnnotationAttributes localAttribs =
metadata.getAnnotationAttributes(targetAnnotationType, classValuesAsString); AnnotationAttributes.fromMap(metadata.getAnnotationAttributes(targetAnnotationType, classValuesAsString));
if (localAttribs != null) { if (localAttribs != null) {
allAttribs.add(localAttribs); allAttribs.add(localAttribs);
} }

View File

@ -27,6 +27,7 @@ import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.EnableLoadTimeWeaving.AspectJWeaving; import org.springframework.context.annotation.EnableLoadTimeWeaving.AspectJWeaving;
import org.springframework.context.weaving.AspectJWeavingEnabler; import org.springframework.context.weaving.AspectJWeavingEnabler;
import org.springframework.context.weaving.DefaultContextLoadTimeWeaver; import org.springframework.context.weaving.DefaultContextLoadTimeWeaver;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata; import org.springframework.core.type.AnnotationMetadata;
import org.springframework.instrument.classloading.LoadTimeWeaver; import org.springframework.instrument.classloading.LoadTimeWeaver;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@ -46,7 +47,7 @@ import org.springframework.util.Assert;
@Configuration @Configuration
public class LoadTimeWeavingConfiguration implements ImportAware, BeanClassLoaderAware { public class LoadTimeWeavingConfiguration implements ImportAware, BeanClassLoaderAware {
private Map<String, Object> enableLTW; private AnnotationAttributes enableLTW;
@Autowired(required=false) @Autowired(required=false)
private LoadTimeWeavingConfigurer ltwConfigurer; private LoadTimeWeavingConfigurer ltwConfigurer;
@ -54,7 +55,7 @@ public class LoadTimeWeavingConfiguration implements ImportAware, BeanClassLoade
private ClassLoader beanClassLoader; private ClassLoader beanClassLoader;
public void setImportMetadata(AnnotationMetadata importMetadata) { public void setImportMetadata(AnnotationMetadata importMetadata) {
this.enableLTW = importMetadata.getAnnotationAttributes(EnableLoadTimeWeaving.class.getName(), false); this.enableLTW = MetadataUtils.attributesFor(importMetadata, EnableLoadTimeWeaving.class);
Assert.notNull(this.enableLTW, Assert.notNull(this.enableLTW,
"@EnableLoadTimeWeaving is not present on importing class " + "@EnableLoadTimeWeaving is not present on importing class " +
importMetadata.getClassName()); importMetadata.getClassName());
@ -79,7 +80,7 @@ public class LoadTimeWeavingConfiguration implements ImportAware, BeanClassLoade
loadTimeWeaver = new DefaultContextLoadTimeWeaver(this.beanClassLoader); loadTimeWeaver = new DefaultContextLoadTimeWeaver(this.beanClassLoader);
} }
switch ((AspectJWeaving) this.enableLTW.get("aspectjWeaving")) { switch (this.enableLTW.getEnum("aspectjWeaving", AspectJWeaving.class)) {
case DISABLED: case DISABLED:
// AJ weaving is disabled -> do nothing // AJ weaving is disabled -> do nothing
break; break;

View File

@ -1,36 +0,0 @@
/*
* Copyright 2002-2011 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.context.annotation;
import org.springframework.core.type.AnnotationMetadata;
class ProfileHelper {
/**
* Return whether the given metadata includes Profile information, whether directly or
* through meta-annotation.
*/
static boolean isProfileAnnotationPresent(AnnotationMetadata metadata) {
return metadata.isAnnotated(Profile.class.getName());
}
/**
* Return the String[] of candidate profiles from {@link Profile#value()}.
*/
static String[] getCandidateProfiles(AnnotationMetadata metadata) {
return (String[])metadata.getAnnotationAttributes(Profile.class.getName()).get("value");
}
}

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2011 the original author or authors. * Copyright 2002-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,12 +17,12 @@
package org.springframework.scheduling.annotation; package org.springframework.scheduling.annotation;
import java.util.Collection; import java.util.Collection;
import java.util.Map;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportAware; import org.springframework.context.annotation.ImportAware;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata; import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@ -37,12 +37,13 @@ import org.springframework.util.Assert;
@Configuration @Configuration
public abstract class AbstractAsyncConfiguration implements ImportAware { public abstract class AbstractAsyncConfiguration implements ImportAware {
protected Map<String, Object> enableAsync; protected AnnotationAttributes enableAsync;
protected Executor executor; protected Executor executor;
public void setImportMetadata(AnnotationMetadata importMetadata) { public void setImportMetadata(AnnotationMetadata importMetadata) {
enableAsync = importMetadata.getAnnotationAttributes(EnableAsync.class.getName(), false); this.enableAsync = AnnotationAttributes.fromMap(
Assert.notNull(enableAsync, importMetadata.getAnnotationAttributes(EnableAsync.class.getName(), false));
Assert.notNull(this.enableAsync,
"@EnableAsync is not present on importing class " + "@EnableAsync is not present on importing class " +
importMetadata.getClassName()); importMetadata.getClassName());
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2011 the original author or authors. * Copyright 2002-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -42,13 +42,11 @@ public class ProxyAsyncConfiguration extends AbstractAsyncConfiguration {
@Bean(name=AnnotationConfigUtils.ASYNC_ANNOTATION_PROCESSOR_BEAN_NAME) @Bean(name=AnnotationConfigUtils.ASYNC_ANNOTATION_PROCESSOR_BEAN_NAME)
@Role(BeanDefinition.ROLE_INFRASTRUCTURE) @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public AsyncAnnotationBeanPostProcessor asyncAdvisor() { public AsyncAnnotationBeanPostProcessor asyncAdvisor() {
Assert.notNull(enableAsync, "@EnableAsync annotation metadata was not injected"); Assert.notNull(this.enableAsync, "@EnableAsync annotation metadata was not injected");
AsyncAnnotationBeanPostProcessor bpp = new AsyncAnnotationBeanPostProcessor(); AsyncAnnotationBeanPostProcessor bpp = new AsyncAnnotationBeanPostProcessor();
@SuppressWarnings("unchecked") Class<? extends Annotation> customAsyncAnnotation = enableAsync.getClass("annotation", Annotation.class);
Class<? extends Annotation> customAsyncAnnotation =
(Class<? extends Annotation>) enableAsync.get("annotation");
if (customAsyncAnnotation != AnnotationUtils.getDefaultValue(EnableAsync.class, "annotation")) { if (customAsyncAnnotation != AnnotationUtils.getDefaultValue(EnableAsync.class, "annotation")) {
bpp.setAsyncAnnotationType(customAsyncAnnotation); bpp.setAsyncAnnotationType(customAsyncAnnotation);
} }
@ -57,9 +55,9 @@ public class ProxyAsyncConfiguration extends AbstractAsyncConfiguration {
bpp.setExecutor(this.executor); bpp.setExecutor(this.executor);
} }
bpp.setProxyTargetClass((Boolean) enableAsync.get("proxyTargetClass")); bpp.setProxyTargetClass(this.enableAsync.getBoolean("proxyTargetClass"));
bpp.setOrder(((Integer) enableAsync.get("order"))); bpp.setOrder(this.enableAsync.getInt("order"));
return bpp; return bpp;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2011 the original author or authors. * Copyright 2002-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -19,18 +19,19 @@ package org.springframework.context.annotation;
import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.springframework.context.annotation.MetadataUtils.attributesFor;
import java.lang.annotation.ElementType; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
import java.util.Map;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata; import org.springframework.core.type.AnnotationMetadata;
import org.springframework.scheduling.annotation.AsyncAnnotationBeanPostProcessor; import org.springframework.scheduling.annotation.AsyncAnnotationBeanPostProcessor;
@ -55,8 +56,8 @@ public class ImportAwareTests {
AnnotationMetadata importMetadata = importAwareConfig.importMetadata; AnnotationMetadata importMetadata = importAwareConfig.importMetadata;
assertThat("import metadata was not injected", importMetadata, notNullValue()); assertThat("import metadata was not injected", importMetadata, notNullValue());
assertThat(importMetadata.getClassName(), is(ImportingConfig.class.getName())); assertThat(importMetadata.getClassName(), is(ImportingConfig.class.getName()));
Map<String, Object> importAttribs = importMetadata.getAnnotationAttributes(Import.class.getName()); AnnotationAttributes importAttribs = attributesFor(importMetadata, Import.class);
Class<?>[] importedClasses = (Class<?>[])importAttribs.get("value"); Class<?>[] importedClasses = importAttribs.getClassArray("value");
assertThat(importedClasses[0].getName(), is(ImportedConfig.class.getName())); assertThat(importedClasses[0].getName(), is(ImportedConfig.class.getName()));
} }
@ -72,8 +73,8 @@ public class ImportAwareTests {
AnnotationMetadata importMetadata = importAwareConfig.importMetadata; AnnotationMetadata importMetadata = importAwareConfig.importMetadata;
assertThat("import metadata was not injected", importMetadata, notNullValue()); assertThat("import metadata was not injected", importMetadata, notNullValue());
assertThat(importMetadata.getClassName(), is(IndirectlyImportingConfig.class.getName())); assertThat(importMetadata.getClassName(), is(IndirectlyImportingConfig.class.getName()));
Map<String, Object> enableAttribs = importMetadata.getAnnotationAttributes(EnableImportedConfig.class.getName()); AnnotationAttributes enableAttribs = attributesFor(importMetadata, EnableImportedConfig.class);
String foo = (String)enableAttribs.get("foo"); String foo = enableAttribs.getString("foo");
assertThat(foo, is("xyz")); assertThat(foo, is("xyz"));
} }
@ -129,7 +130,6 @@ public class ImportAwareTests {
} }
public void setBeanFactory(BeanFactory beanFactory) throws BeansException { public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("ImportAwareTests.BPP.setBeanFactory()");
} }
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2011 the original author or authors. * Copyright 2002-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,11 +17,11 @@
package org.springframework.transaction.annotation; package org.springframework.transaction.annotation;
import java.util.Collection; import java.util.Collection;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportAware; import org.springframework.context.annotation.ImportAware;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata; import org.springframework.core.type.AnnotationMetadata;
import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@ -37,11 +37,12 @@ import org.springframework.util.Assert;
@Configuration @Configuration
public abstract class AbstractTransactionManagementConfiguration implements ImportAware { public abstract class AbstractTransactionManagementConfiguration implements ImportAware {
protected Map<String, Object> enableTx; protected AnnotationAttributes enableTx;
protected PlatformTransactionManager txManager; protected PlatformTransactionManager txManager;
public void setImportMetadata(AnnotationMetadata importMetadata) { public void setImportMetadata(AnnotationMetadata importMetadata) {
this.enableTx = importMetadata.getAnnotationAttributes(EnableTransactionManagement.class.getName(), false); this.enableTx = AnnotationAttributes.fromMap(
importMetadata.getAnnotationAttributes(EnableTransactionManagement.class.getName(), false));
Assert.notNull(this.enableTx, Assert.notNull(this.enableTx,
"@EnableTransactionManagement is not present on importing class " + "@EnableTransactionManagement is not present on importing class " +
importMetadata.getClassName()); importMetadata.getClassName());

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2011 the original author or authors. * Copyright 2002-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -44,7 +44,7 @@ public class ProxyTransactionManagementConfiguration extends AbstractTransaction
new BeanFactoryTransactionAttributeSourceAdvisor(); new BeanFactoryTransactionAttributeSourceAdvisor();
advisor.setTransactionAttributeSource(transactionAttributeSource()); advisor.setTransactionAttributeSource(transactionAttributeSource());
advisor.setAdvice(transactionInterceptor()); advisor.setAdvice(transactionInterceptor());
advisor.setOrder(((Integer)this.enableTx.get("order"))); advisor.setOrder(this.enableTx.getInt("order"));
return advisor; return advisor;
} }