Skip to content

Conflicting Conditional bean behavior #24156

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

Closed
JigarJoshi opened this issue Dec 6, 2019 · 1 comment
Closed

Conflicting Conditional bean behavior #24156

JigarJoshi opened this issue Dec 6, 2019 · 1 comment
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: duplicate A duplicate of another issue

Comments

@JigarJoshi
Copy link

JigarJoshi commented Dec 6, 2019

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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Dec 6, 2019
@sbrannen
Copy link
Member

sbrannen commented 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.

@sbrannen sbrannen closed this as completed Dec 7, 2019
@sbrannen sbrannen self-assigned this Dec 7, 2019
@sbrannen sbrannen added status: duplicate A duplicate of another issue in: core Issues in core modules (aop, beans, core, context, expression) and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Dec 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

3 participants