From 5dddb492b85b197a8f0e17a53c102ad96037fe1b Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 23 Dec 2013 23:09:00 +0100 Subject: [PATCH] Polishing around @Enable processing and @EnableTransactionManagement Issue: SPR-11251 --- .../annotation/AdviceModeImportSelector.java | 21 +++++-------------- .../annotation/AutoProxyRegistrar.java | 20 ++++++++---------- ...onfigurationClassBeanDefinitionReader.java | 19 ++++++++--------- .../EnableTransactionManagement.java | 4 ++-- ...oxyTransactionManagementConfiguration.java | 5 ++--- ...actionManagementConfigurationSelector.java | 3 +-- 6 files changed, 28 insertions(+), 44 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/context/annotation/AdviceModeImportSelector.java b/spring-context/src/main/java/org/springframework/context/annotation/AdviceModeImportSelector.java index eae51ec2516..c736bdad570 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/AdviceModeImportSelector.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/AdviceModeImportSelector.java @@ -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 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 implements } /** - * {@inheritDoc} - * - *

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}. - * *

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}. - * *

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. */ diff --git a/spring-context/src/main/java/org/springframework/context/annotation/AutoProxyRegistrar.java b/spring-context/src/main/java/org/springframework/context/annotation/AutoProxyRegistrar.java index 5fcd8a572d0..e8a8727793f 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/AutoProxyRegistrar.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/AutoProxyRegistrar.java @@ -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. - * *

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 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)); } } + } diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassBeanDefinitionReader.java b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassBeanDefinitionReader.java index 5250f5e34c6..3824467bae3 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassBeanDefinitionReader.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassBeanDefinitionReader.java @@ -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.getNumber("value")); } // consider name and any aliases - AnnotationAttributes bean = attributesFor(metadata, Bean.class); + AnnotationAttributes bean = MetadataUtils.attributesFor(metadata, Bean.class); List names = new ArrayList(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); diff --git a/spring-tx/src/main/java/org/springframework/transaction/annotation/EnableTransactionManagement.java b/spring-tx/src/main/java/org/springframework/transaction/annotation/EnableTransactionManagement.java index aa51af07e74..565e857f15e 100644 --- a/spring-tx/src/main/java/org/springframework/transaction/annotation/EnableTransactionManagement.java +++ b/spring-tx/src/main/java/org/springframework/transaction/annotation/EnableTransactionManagement.java @@ -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. @@ -145,7 +145,6 @@ public @interface EnableTransactionManagement { * opposed to standard Java interface-based proxies ({@code false}). The default is * {@code false}. Applicable only if {@link #mode()} is set to * {@link AdviceMode#PROXY}. - * *

Note that setting this attribute to {@code true} will affect all * Spring-managed beans requiring proxying, not just those marked with * {@code @Transactional}. For example, other beans marked with Spring's @@ -168,4 +167,5 @@ public @interface EnableTransactionManagement { * The default is {@link Ordered#LOWEST_PRECEDENCE}. */ int order() default Ordered.LOWEST_PRECEDENCE; + } diff --git a/spring-tx/src/main/java/org/springframework/transaction/annotation/ProxyTransactionManagementConfiguration.java b/spring-tx/src/main/java/org/springframework/transaction/annotation/ProxyTransactionManagementConfiguration.java index c1602479b4e..40a86373456 100644 --- a/spring-tx/src/main/java/org/springframework/transaction/annotation/ProxyTransactionManagementConfiguration.java +++ b/spring-tx/src/main/java/org/springframework/transaction/annotation/ProxyTransactionManagementConfiguration.java @@ -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. @@ -40,8 +40,7 @@ public class ProxyTransactionManagementConfiguration extends AbstractTransaction @Bean(name=TransactionManagementConfigUtils.TRANSACTION_ADVISOR_BEAN_NAME) @Role(BeanDefinition.ROLE_INFRASTRUCTURE) public BeanFactoryTransactionAttributeSourceAdvisor transactionAdvisor() { - BeanFactoryTransactionAttributeSourceAdvisor advisor = - new BeanFactoryTransactionAttributeSourceAdvisor(); + BeanFactoryTransactionAttributeSourceAdvisor advisor = new BeanFactoryTransactionAttributeSourceAdvisor(); advisor.setTransactionAttributeSource(transactionAttributeSource()); advisor.setAdvice(transactionInterceptor()); advisor.setOrder(this.enableTx.getNumber("order")); diff --git a/spring-tx/src/main/java/org/springframework/transaction/annotation/TransactionManagementConfigurationSelector.java b/spring-tx/src/main/java/org/springframework/transaction/annotation/TransactionManagementConfigurationSelector.java index b82c96e0668..145791f7a29 100644 --- a/spring-tx/src/main/java/org/springframework/transaction/annotation/TransactionManagementConfigurationSelector.java +++ b/spring-tx/src/main/java/org/springframework/transaction/annotation/TransactionManagementConfigurationSelector.java @@ -32,8 +32,7 @@ import org.springframework.transaction.config.TransactionManagementConfigUtils; * @see ProxyTransactionManagementConfiguration * @see TransactionManagementConfigUtils#TRANSACTION_ASPECT_CONFIGURATION_CLASS_NAME */ -public class TransactionManagementConfigurationSelector - extends AdviceModeImportSelector { +public class TransactionManagementConfigurationSelector extends AdviceModeImportSelector { /** * {@inheritDoc}