Closed
Description
package com.example.springbootdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
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;
@SpringBootApplication
public class SpringBootDemoApplication {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(SpringBootDemoApplication.class, args);
System.out.println("Bean found ? " + (ctx.getBean(Foo.class) != null));
}
}
@Configuration
class TestConfiguration {
@Bean
@Conditional(AlwaysMismatch.class)
public Foo foo(int dummy) {
return new Foo();
}
@Bean
@Conditional(AlwaysMatch.class)
public Foo foo() {
return new Foo();
}
}
class AlwaysMatch implements Condition {
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
return true;
}
}
class AlwaysMismatch implements Condition {
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
return false;
}
}
class Foo {
}
Expectation
With above code I expect one bean of type com.example.springbootdemo.Foo
contributed with bean-name foo
.
Actual behavior
No bean of type com.example.springbootdemo.Foo
is registered.
With a slight change in above code.
From
@Configuration
class TestConfiguration {
@Bean
@Conditional(AlwaysMismatch.class)
public Foo foo(int dummy) {
return new Foo();
}
@Bean
@Conditional(AlwaysMatch.class)
public Foo foo() {
return new Foo();
}
}
to
@Configuration
class TestConfiguration {
@Bean
@Conditional(AlwaysMismatch.class)
public Foo foo(int dummy) {
return new Foo();
}
@Bean
@Conditional(AlwaysMatch.class)
public Foo foo1() {
return new Foo();
}
}
bean of type com.example.springbootdemo.Foo
is registered.
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
sbrannen commentedon Dec 7, 2019
Related to #17292 and #17341.
Furthermore, I am closing this as a duplicate of #19831.
In summary, competing conditions are not supported for overloaded
@Bean
methods (i.e., methods with the same name). See the comments in #19831 for further details.