Giter VIP home page Giter VIP logo

-conditionalonbean-solve's Introduction

If you have two classes implementing CommandLineRunner, and you want to use only one of them in your Spring Boot application, you can use the @Primary annotation on the bean that you want to use, just like we did in the previous answer.

Here's an example:

@Configuration
public class AppConfig {
    @Bean
    @Primary
    public MyFirstCommandLineRunner myFirstCommandLineRunner() {
        return new MyFirstCommandLineRunner();
    }

    @Bean
    public MySecondCommandLineRunner mySecondCommandLineRunner() {
        return new MySecondCommandLineRunner();
    }
}

In this example, we have two classes that implement CommandLineRunner: MyFirstCommandLineRunner and MySecondCommandLineRunner. We want to use MyFirstCommandLineRunner in our Spring Boot application, so we've annotated the myFirstCommandLineRunner() method with @Primary.

Now, when we run our Spring Boot application, Spring will use the myFirstCommandLineRunner() method to create the bean, since it is annotated with @Primary.

Here's an example of how you can use @ConditionalOnBean to conditionally create a bean based on the presence of another bean:

@Configuration
public class AppConfig {
    @Bean
    public MyService myService() {
        return new MyService();
    }

    @Bean
    @ConditionalOnBean(MyService.class)
    public MyCommandLineRunner myCommandLineRunner() {
        return new MyCommandLineRunner();
    }
}

In this example, we have a MyService bean and a MyCommandLineRunner bean. We only want to create the MyCommandLineRunner bean if the MyService bean is present.

We've annotated the myCommandLineRunner() method with @ConditionalOnBean(MyService.class). This means that Spring will only create the myCommandLineRunner() bean if the MyService bean is already present in the application context.

If the MyService bean is not present, the myCommandLineRunner() bean will not be created.

-conditionalonbean-solve's People

Contributors

amideb avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.