From c5463a2e52b432c0347da895d7e711079a4af969 Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Wed, 13 Jul 2011 23:28:53 +0000 Subject: [PATCH] Move ImportSelector.Context to a top-level class Issue: SPR-8411, SPR-8494 --- .../annotation/ConfigurationClassParser.java | 2 +- .../context/annotation/Import.java | 18 ++++---- .../context/annotation/ImportSelector.java | 41 +++---------------- .../annotation/ImportSelectorContext.java | 33 +++++++++++++++ .../AsyncConfigurationSelector.java | 3 +- ...actionManagementConfigurationSelector.java | 3 +- 6 files changed, 52 insertions(+), 48 deletions(-) create mode 100644 org.springframework.context/src/main/java/org/springframework/context/annotation/ImportSelectorContext.java diff --git a/org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java b/org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java index a09f033833e..306b957e789 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java +++ b/org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java @@ -248,7 +248,7 @@ class ConfigurationClassParser { // the candidate class is an ImportSelector -> delegate to it to determine imports try { ImportSelector selector = BeanUtils.instantiateClass(Class.forName(candidate), ImportSelector.class); - ImportSelector.Context context = new ImportSelector.Context(importingClassMetadata, this.registry); + ImportSelectorContext context = new ImportSelectorContext(importingClassMetadata, this.registry); processImport(configClass, selector.selectImports(context), false); } catch (ClassNotFoundException ex) { throw new IllegalStateException(ex); diff --git a/org.springframework.context/src/main/java/org/springframework/context/annotation/Import.java b/org.springframework.context/src/main/java/org/springframework/context/annotation/Import.java index 36d6d557321..ce0ea878583 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/annotation/Import.java +++ b/org.springframework.context/src/main/java/org/springframework/context/annotation/Import.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * 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. @@ -23,26 +23,26 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** - * Indicates one or more @{@link @Configuration} classes to import. + * Indicates one or more @{@link Configuration} classes to import. * *

Provides functionality equivalent to the {@code } element in Spring XML. * Only supported for actual {@code @Configuration}-annotated classes and implementations * of the {@link ImportSelector} interface. * - *

{@link Bean @Bean} definitions declared in imported {@code @Configuration} classes - * should be accessed by using {@link Autowired @Autowired} injection. Either the bean - * itself can be autowired, or the configuration class instance declaring the bean can be - * autowired. The latter approach allows for explicit, IDE-friendly navigation between + *

@{@link Bean} definitions declared in imported {@code @Configuration} classes + * should be accessed by using @{@link Autowired} injection. Either the bean itself can + * be autowired, or the configuration class instance declaring the bean can be autowired. + * The latter approach allows for explicit, IDE-friendly navigation between * {@code @Configuration} class methods. * *

If XML or other non-{@code @Configuration} bean definition resources need to be - * imported, use {@link ImportResource @ImportResource} + * imported, use @{@link ImportResource} * * @author Chris Beams * @since 3.0 * @see Configuration - * @see ImportResource * @see ImportSelector + * @see ImportResource */ @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @@ -50,7 +50,7 @@ import java.lang.annotation.Target; public @interface Import { /** - * The {@link Configuration} class or classes to import. + * The @{@link Configuration} and/or {@link ImportSelector} classes to import. */ Class[] value(); } diff --git a/org.springframework.context/src/main/java/org/springframework/context/annotation/ImportSelector.java b/org.springframework.context/src/main/java/org/springframework/context/annotation/ImportSelector.java index 76920bc97e9..a4d8b61b706 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/annotation/ImportSelector.java +++ b/org.springframework.context/src/main/java/org/springframework/context/annotation/ImportSelector.java @@ -16,17 +16,14 @@ package org.springframework.context.annotation; -import org.springframework.beans.factory.support.BeanDefinitionRegistry; -import org.springframework.core.type.AnnotationMetadata; - /** * Interface to be implemented by types that determine which @{@link Configuration} * class(es) should be imported based on a given selection criteria, usually one or more * annotation attributes. * *

In certain cases, an {@code ImportSelector} may register additional bean definitions - * through the {@link BeanDefinitionRegistry} available in the - * {@code Context} provided to the {@link #selectImports} method. + * through the {@code BeanDefinitionRegistry} available in the + * {@link ImportSelectorContext} provided to the {@link #selectImports} method. * * @author Chris Beams * @since 3.1 @@ -40,37 +37,9 @@ public interface ImportSelector { * the {@code AnnotationMetadata} of the importing {@code @Configuration} class and * optionally register any {@code BeanDefinition}s necessary to support the selected * classes. - * @param context containing the AnnotationMetadata of the importing @{@link - * Configuration} class and the enclosing {@link BeanDefinitionRegistry}. + * @param context containing the {@code AnnotationMetadata} of the importing @{@link + * Configuration} class and the enclosing {@code BeanDefinitionRegistry}. */ - String[] selectImports(Context context); - - - /** - * Context object holding the {@link AnnotationMetadata} of the {@code @Configuration} - * class that imported this {@link ImportSelector} as well as the enclosing - * {@link BeanDefinitionRegistry} to allow for conditional bean definition - * registration when necessary. - * - * @author Chris Beams - * @since 3.1 - */ - static class Context { - private final AnnotationMetadata importingClassMetadata; - private final BeanDefinitionRegistry registry; - - public Context(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { - this.importingClassMetadata = importingClassMetadata; - this.registry = registry; - } - - public AnnotationMetadata getImportingClassMetadata() { - return this.importingClassMetadata; - } - - public BeanDefinitionRegistry getBeanDefinitionRegistry() { - return registry; - } - } + String[] selectImports(ImportSelectorContext context); } diff --git a/org.springframework.context/src/main/java/org/springframework/context/annotation/ImportSelectorContext.java b/org.springframework.context/src/main/java/org/springframework/context/annotation/ImportSelectorContext.java new file mode 100644 index 00000000000..ef23c63f9c9 --- /dev/null +++ b/org.springframework.context/src/main/java/org/springframework/context/annotation/ImportSelectorContext.java @@ -0,0 +1,33 @@ +package org.springframework.context.annotation; + +import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.core.type.AnnotationMetadata; + +/** + * Context object holding the {@link AnnotationMetadata} of the @{@link Configuration} + * class that imported the current {@link ImportSelector} as well as the enclosing + * {@link BeanDefinitionRegistry} to allow for conditional bean definition + * registration when necessary. + * + * @author Chris Beams + * @since 3.1 + * @see Import + * @see ImportSelector + */ +public class ImportSelectorContext { + private final AnnotationMetadata importingClassMetadata; + private final BeanDefinitionRegistry registry; + + ImportSelectorContext(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { + this.importingClassMetadata = importingClassMetadata; + this.registry = registry; + } + + public AnnotationMetadata getImportingClassMetadata() { + return this.importingClassMetadata; + } + + public BeanDefinitionRegistry getBeanDefinitionRegistry() { + return registry; + } +} \ No newline at end of file diff --git a/org.springframework.context/src/main/java/org/springframework/scheduling/annotation/AsyncConfigurationSelector.java b/org.springframework.context/src/main/java/org/springframework/scheduling/annotation/AsyncConfigurationSelector.java index 9f5791b46f6..8cfd0a103bd 100644 --- a/org.springframework.context/src/main/java/org/springframework/scheduling/annotation/AsyncConfigurationSelector.java +++ b/org.springframework.context/src/main/java/org/springframework/scheduling/annotation/AsyncConfigurationSelector.java @@ -19,6 +19,7 @@ package org.springframework.scheduling.annotation; import java.util.Map; import org.springframework.context.annotation.AnnotationConfigUtils; +import org.springframework.context.annotation.ImportSelectorContext; import org.springframework.context.annotation.ImportSelector; import org.springframework.context.config.AdviceMode; import org.springframework.core.type.AnnotationMetadata; @@ -46,7 +47,7 @@ public class AsyncConfigurationSelector implements ImportSelector { * AspectJAsyncConfiguration}. No additional {@code BeanDefinition}s are registered * in either case. */ - public String[] selectImports(ImportSelector.Context context) { + public String[] selectImports(ImportSelectorContext context) { AnnotationMetadata importingClassMetadata = context.getImportingClassMetadata(); Map enableAsync = importingClassMetadata.getAnnotationAttributes(EnableAsync.class.getName()); diff --git a/org.springframework.transaction/src/main/java/org/springframework/transaction/annotation/TransactionManagementConfigurationSelector.java b/org.springframework.transaction/src/main/java/org/springframework/transaction/annotation/TransactionManagementConfigurationSelector.java index 56290a8c00b..c04a37c2002 100644 --- a/org.springframework.transaction/src/main/java/org/springframework/transaction/annotation/TransactionManagementConfigurationSelector.java +++ b/org.springframework.transaction/src/main/java/org/springframework/transaction/annotation/TransactionManagementConfigurationSelector.java @@ -21,6 +21,7 @@ import java.util.Map; import org.springframework.aop.config.AopConfigUtils; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.ImportSelectorContext; import org.springframework.context.annotation.ImportSelector; import org.springframework.context.config.AdviceMode; import org.springframework.core.type.AnnotationMetadata; @@ -50,7 +51,7 @@ public class TransactionManagementConfigurationSelector implements ImportSelecto * will also be added to the enclosing {@link BeanDefinitionRegistry} and escalated * if necessary through the usual {@link AopConfigUtils} family of methods. */ - public String[] selectImports(ImportSelector.Context context) { + public String[] selectImports(ImportSelectorContext context) { AnnotationMetadata importingClassMetadata = context.getImportingClassMetadata(); BeanDefinitionRegistry registry = context.getBeanDefinitionRegistry();