Skip to content

A configuration class superclass that is skipped due to register bean phase conditions is ignored when another configuration class that extends it is processed #28676

Closed

Description

@wilkinsona

Affects: 6.0.x. I expect 5.3.x to also be affected.

I think @mbhave and I have found a bug in configuration class processing related to condition evaluation. It's hopefully illustrated by the following tests:

import org.junit.jupiter.api.Test;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ConfigurationCondition;
import org.springframework.context.annotation.Import;
import org.springframework.core.type.AnnotatedTypeMetadata;

import static org.assertj.core.api.Assertions.assertThat;

class ConfigurationPhasesKnownSuperclassesTests {

	@Test
	void superclassSkippedInParseConfigurationPhaseShouldNotPreventSubsequentProcessingOfSameSuperclass() {
		try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
				ParseConfigurationPhase.class)) {
			assertThat(context.getBean("subclassBean")).isEqualTo("bravo");
			assertThat(context.getBean("superclassBean")).isEqualTo("superclass");
		}
	}

	@Test
	void superclassSkippedInRegisterBeanPhaseShouldNotPreventSubsequentProcessingOfSameSuperclass() {
		try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
				RegisterBeanPhase.class)) {
			assertThat(context.getBean("subclassBean")).isEqualTo("bravo");
			assertThat(context.getBean("superclassBean")).isEqualTo("superclass");
		}
	}

	@Configuration(proxyBeanMethods = false)
	static class Example {

		@Bean
		String superclassBean() {
			return "superclass";
		}

	}

	@Configuration(proxyBeanMethods = false)
	@Import({ RegisterBeanPhaseExample.class, BravoExample.class })
	static class RegisterBeanPhase {

	}

	@Conditional(NonMatchingRegisterBeanPhaseCondition.class)
	@Configuration(proxyBeanMethods = false)
	static class RegisterBeanPhaseExample extends Example {

		@Bean
		String subclassBean() {
			return "alpha";
		}

	}

	@Configuration(proxyBeanMethods = false)
	@Import({ ParseConfigurationPhaseExample.class, BravoExample.class })
	static class ParseConfigurationPhase {

	}

	@Conditional(NonMatchingParseConfigurationPhaseCondition.class)
	@Configuration(proxyBeanMethods = false)
	static class ParseConfigurationPhaseExample extends Example {

		@Bean
		String subclassBean() {
			return "alpha";
		}

	}

	@Configuration(proxyBeanMethods = false)
	static class BravoExample extends Example {

		@Bean
		String subclassBean() {
			return "bravo";
		}

	}

	static class NonMatchingRegisterBeanPhaseCondition implements ConfigurationCondition {

		@Override
		public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
			return false;
		}

		@Override
		public ConfigurationPhase getConfigurationPhase() {
			return ConfigurationPhase.REGISTER_BEAN;
		}

	}

	static class NonMatchingParseConfigurationPhaseCondition implements ConfigurationCondition {

		@Override
		public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
			return false;
		}

		@Override
		public ConfigurationPhase getConfigurationPhase() {
			return ConfigurationPhase.PARSE_CONFIGURATION;
		}

	}

}

superclassSkippedInParseConfigurationPhaseShouldNotPreventSubsequentProcessingOfSameSuperclass passes but superclassSkippedInRegisterBeanPhaseShouldNotPreventSubsequentProcessingOfSameSuperclass fails.

The register bean phase test fails due to the knownSuperclasses map in ConfigurationClassParser being polluted. Due to the parse configuration phase conditions matching an entry is added to the map for Example -> RegisterBeanPhaseExample. Subsequently, the register bean phase condition on RegisterBeanPhaseExample does not match, so neither it nor Example are processed. When BravoExample is then parsed, ConfigurationClassParser considers its superclass Example. It's skipped due to the existing entry in the knownSuperclasses map, despite the fact that Example was never actually processed due to the conditions on RegisterBeanPhaseExample. The end result is that the context is left without a bean named superclassBean.

Activity

  1. added
    status: waiting-for-triage
    on Jun 22, 2022
  2. added
    in: core
    on Feb 9, 2023
  3. self-assigned this
    on Dec 29, 2023
  4. added
    type: bug
    and removed
    status: waiting-for-triage
    on Dec 29, 2023
  5. added this to the 6.2.x milestone on Dec 29, 2023
  6. modified the milestones: 6.2.x, 6.2.0-M1 on Jan 12, 2024
  7. added a commit that references this issue on Feb 21, 2024
    ff9c714
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: coretype: bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions