@Import target is treated as @Configuration in spring

I was creating a java config class that would be @ Import in spring. At this time, it is strange that `@ Bean``` in the class is registered if @ Import``` is done without adding `` @ Configuration``` to the setting class. So I took a look at the source code to study that area.

pom.xml


	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.3.RELEASE</version>
	</parent>

If you follow the source, I will omit the details of ApplicationContext, but it seems that it is around here that `` `@ Import``` is processed. In the following method, a collection of import candidate classes is passed and processed one by one.

ConfigurationClassParser


package org.springframework.context.annotation;

class ConfigurationClassParser {
	private void processImports(/*...(Abbreviation)...*/, Collection<SourceClass> importCandidates, /*...(Abbreviation)...*/) {
				// ...(Abbreviation)
				for (SourceClass candidate : importCandidates) {
					if (candidate.isAssignable(ImportSelector.class)) {
						// ...(Abbreviation)
					else if (candidate.isAssignable(ImportBeanDefinitionRegistrar.class)) {
						// ...(Abbreviation)
					else {
						// Candidate class not an ImportSelector or ImportBeanDefinitionRegistrar ->
						// process it as an @Configuration class
						this.importStack.registerImport(
								currentSourceClass.getMetadata(), candidate.getMetadata().getClassName());
						processConfigurationClass(candidate.asConfigClass(configClass));

At this time, if `@ ImportSelector``` and @ ImportBeanDefinitionRegistrar``` to perform conditional import are assigned, the processing dedicated to that is performed. However, if it is not those annotations, as stated in the comment in the code, "If the import candidate class is not ImportSelector or ImportBeanDefinitionRegistrar, it will be processed as `@ Configuration```. "

So, it seems basically good to think that the `` `@ Importtarget class is regarded as @ Configuration```. Of course, there will be various differences in detail.

Recommended Posts

@Import target is treated as @Configuration in spring
Event processing is performed in Spring.
Spring Autowired is written in the constructor
Maven configuration problem in Spring pom.xml in Eclipse
File output bean as JSON in spring
[Java] Something is displayed as "-0.0" in the output
When Spring Batch is executed continuously in Oracle, ORA-08177
Java11: Run Java code in a single file as is