Skip to content

A lite configuration class's member classes are processed when it's imported but not when it's registered directly [SPR-16839] #21379

@spring-projects-issues

Description

@spring-projects-issues

Andy Wilkinson opened SPR-16839 and commented

The problem's hopefully illustrated by the following two tests:

package com.example.demo;

import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

public class ImportVersusDirectRegistrationTests {

	@Test
	public void thingIsAvailableWhenOuterConfigurationIsRegisteredDirectly() {
		try (AnnotationConfigApplicationContext directRegistration = new AnnotationConfigApplicationContext()) {
			directRegistration.register(AccidentalLiteConfiguration.class);
			directRegistration.refresh();
			directRegistration.getBean(Thing.class);
		}
	}

	@Test
	public void thingIsAvailableWhenOuterConfigurationIsImported() {
		try (AnnotationConfigApplicationContext viaImport = new AnnotationConfigApplicationContext()) {
			viaImport.register(Importer.class);
			viaImport.refresh();
			viaImport.getBean(Thing.class);
		}
	}

}

@Import(AccidentalLiteConfiguration.class)
class Importer {

}

class AccidentalLiteConfiguration {

	@Configuration
	class InnerConfiguration {

		@Bean
		public Thing thing() {
			return new Thing();
		}

	}

}

class Thing {

}

thingIsAvailableWhenOuterConfigurationIsImported will pass but thingIsAvailableWhenOuterConfigurationIsRegisteredDirectly will fail. This appears to be because member classes of a lite configuration class are only processed when the lite configuration class has been imported. They are not processed when it's been registered directly with the application context.


Affects: 5.0.6

Reference URL: spring-projects/spring-boot#13129

Issue Links:

Referenced from: commits spring-projects/spring-boot@5cc1a83

0 votes, 5 watchers

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions