@Import allows for importing regular component classes as well
Issue: SPR-11740
This commit is contained in:
@@ -35,8 +35,6 @@ import org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostPr
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.config.BeanDefinitionHolder;
|
||||
import org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader;
|
||||
import org.springframework.beans.factory.parsing.Location;
|
||||
import org.springframework.beans.factory.parsing.Problem;
|
||||
import org.springframework.beans.factory.parsing.ProblemReporter;
|
||||
import org.springframework.beans.factory.parsing.SourceExtractor;
|
||||
import org.springframework.beans.factory.support.AbstractBeanDefinitionReader;
|
||||
@@ -160,22 +158,18 @@ class ConfigurationClassBeanDefinitionReader {
|
||||
AnnotationMetadata metadata = configClass.getMetadata();
|
||||
AnnotatedGenericBeanDefinition configBeanDef = new AnnotatedGenericBeanDefinition(metadata);
|
||||
|
||||
if (ConfigurationClassUtils.checkConfigurationClassCandidate(configBeanDef, this.metadataReaderFactory)) {
|
||||
ScopeMetadata scopeMetadata = scopeMetadataResolver.resolveScopeMetadata(configBeanDef);
|
||||
configBeanDef.setScope(scopeMetadata.getScopeName());
|
||||
String configBeanName = this.importBeanNameGenerator.generateBeanName(configBeanDef, this.registry);
|
||||
AnnotationConfigUtils.processCommonDefinitionAnnotations(configBeanDef, metadata);
|
||||
BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(configBeanDef, configBeanName);
|
||||
definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
|
||||
this.registry.registerBeanDefinition(definitionHolder.getBeanName(), definitionHolder.getBeanDefinition());
|
||||
configClass.setBeanName(configBeanName);
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(String.format("Registered bean definition for imported @Configuration class %s", configBeanName));
|
||||
}
|
||||
}
|
||||
else {
|
||||
this.problemReporter.error(
|
||||
new InvalidConfigurationImportProblem(metadata.getClassName(), configClass.getResource(), metadata));
|
||||
ScopeMetadata scopeMetadata = scopeMetadataResolver.resolveScopeMetadata(configBeanDef);
|
||||
configBeanDef.setScope(scopeMetadata.getScopeName());
|
||||
String configBeanName = this.importBeanNameGenerator.generateBeanName(configBeanDef, this.registry);
|
||||
AnnotationConfigUtils.processCommonDefinitionAnnotations(configBeanDef, metadata);
|
||||
|
||||
BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(configBeanDef, configBeanName);
|
||||
definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
|
||||
this.registry.registerBeanDefinition(definitionHolder.getBeanName(), definitionHolder.getBeanDefinition());
|
||||
configClass.setBeanName(configBeanName);
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Registered bean definition for imported class '" + configBeanName + "'");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -422,21 +416,6 @@ class ConfigurationClassBeanDefinitionReader {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Configuration classes must be annotated with {@link Configuration @Configuration} or
|
||||
* declare at least one {@link Bean @Bean} method.
|
||||
*/
|
||||
private static class InvalidConfigurationImportProblem extends Problem {
|
||||
|
||||
public InvalidConfigurationImportProblem(String className, Resource resource, AnnotationMetadata metadata) {
|
||||
super(String.format("%s was @Import'ed but is not annotated with @Configuration " +
|
||||
"nor does it declare any @Bean methods; it does not implement ImportSelector " +
|
||||
"or extend ImportBeanDefinitionRegistrar. Update the class to meet one of these requirements " +
|
||||
"or do not attempt to @Import it.", className), new Location(resource, metadata));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Evaluate {@code @Conditional} annotations, tracking results and taking into
|
||||
* account 'imported by'.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2015 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.
|
||||
@@ -26,22 +26,23 @@ import java.lang.annotation.Target;
|
||||
* Indicates one or more {@link Configuration @Configuration} classes to import.
|
||||
*
|
||||
* <p>Provides functionality equivalent to the {@code <import/>} element in Spring XML.
|
||||
* Only supported for classes annotated with {@code @Configuration} or declaring at least
|
||||
* one {@link Bean @Bean} method, as well as {@link ImportSelector} and
|
||||
* {@link ImportBeanDefinitionRegistrar} implementations.
|
||||
* Allows for importing {@code @Configuration} classes, {@link ImportSelector} and
|
||||
* {@link ImportBeanDefinitionRegistrar} implementations, as well as regular component
|
||||
* classes (as of 4.2; analogous to {@link AnnotationConfigApplicationContext#register}).
|
||||
*
|
||||
* <p>{@code @Bean} definitions declared in imported {@code @Configuration} classes
|
||||
* should be accessed by using {@link org.springframework.beans.factory.annotation.Autowired @Autowired}
|
||||
* <p>{@code @Bean} definitions declared in imported {@code @Configuration} classes should be
|
||||
* accessed by using {@link org.springframework.beans.factory.annotation.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 {@code @Configuration} class methods.
|
||||
* declaring the bean can be autowired. The latter approach allows for explicit, IDE-friendly
|
||||
* navigation between {@code @Configuration} class methods.
|
||||
*
|
||||
* <p>May be declared at the class level or as a meta-annotation.
|
||||
*
|
||||
* <p>If XML or other non-{@code @Configuration} bean definition resources need to be
|
||||
* imported, use {@link ImportResource @ImportResource}
|
||||
* imported, use the {@link ImportResource @ImportResource} annotation instead.
|
||||
*
|
||||
* @author Chris Beams
|
||||
* @author Juergen Hoeller
|
||||
* @since 3.0
|
||||
* @see Configuration
|
||||
* @see ImportSelector
|
||||
@@ -53,8 +54,9 @@ import java.lang.annotation.Target;
|
||||
public @interface Import {
|
||||
|
||||
/**
|
||||
* The @{@link Configuration}, {@link ImportSelector} and/or
|
||||
* {@link ImportBeanDefinitionRegistrar} classes to import.
|
||||
* @{@link Configuration}, {@link ImportSelector}, {@link ImportBeanDefinitionRegistrar}
|
||||
* or regular component classes to import.
|
||||
*/
|
||||
Class<?>[] value();
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user