Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditions on an overriding bean method effectively get ignored [SPR-12694] #17292

Closed
spring-issuemaster opened this issue Feb 5, 2015 · 1 comment

Comments

Projects
None yet
2 participants
@spring-issuemaster
Copy link
Collaborator

commented Feb 5, 2015

Andy Wilkinson opened SPR-12694 and commented

In Spring Boot, I'd like to be able to override a method in a Spring Data REST configuration class to make it conditional, however this doesn't work. The condition on the overriding method is evaluated and correctly considered as not matching but the overridden method is then also considered. It has no conditions so a bean definition is created.

This is illustrated by this test:

import java.util.Map;

import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.type.AnnotatedTypeMetadata;

import static org.junit.Assert.assertEquals;

public class ConditionalOverriddenBeanMethodTests {

	@Test
	public void conditionsOnOverriddenMethodsAreHonoured() {
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
				Bar.class);
		Map<String, Baz> beansOfType = context.getBeansOfType(Baz.class);
		assertEquals(0, beansOfType.size());
	}

	@Configuration
	private static class Foo {

		@Bean
		public Baz baz() {
			return new Baz();
		}

	}

	private static class Bar extends Foo {

		@Override
		@Bean
		@Conditional(NeverMatchesCondition.class)
		public Baz baz() {
			return new Baz();
		}

	}

	private static class Baz {

	}

	private static class NeverMatchesCondition implements Condition {

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

	}

}

My expectation is that the condition will prevent the Baz bean from being created.


Affects: 4.1.4

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

Issue Links:

  • #17341 Regression in 4.1.5: Alternative @Bean declarations with same primary bean name do not work anymore
  • #19831 Doc: Consistent @Profile declarations on overloaded @Bean methods

0 votes, 5 watchers

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Feb 10, 2015

Juergen Hoeller commented

I've revised ConfigurationClassBeanDefinitionReader's registration algorithm to keep track of skipped beans within a configuration class, not allowing registration of the same bean in a superclass thereof. This is bean name based instead of override based since we allow conceptual overriding through a same-named @Bean method but with a different signature.

I'll backport this to the 4.1.5 branch tomorrow.

Juergen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.