How to do it...

  1. In the DbCountAutoConfiguration class, we will add an @ConditionalOnMissingBean annotation to the dbCountRunner(...) method, as follows:
@Bean 
@ConditionalOnMissingBean 
public DbCountRunner 
dbCountRunner(Collection<CrudRepository> repositories) { return new DbCountRunner(repositories); }
  1. We will also need to add a dependency on the spring-boot-autoconfigure artifact to the dependencies section of the db-count-starter/build.gradle file:
compile("org.springframework.boot:spring-boot-autoconfigure:2.0.0.BUILD-SNAPSHOT")
  1. Now, let's start the application by running ./gradlew clean bootRun in order to verify that we will still see the same output in the console logs as we did in the previous recipe
  2. If we start the application with the DEBUG switch so as to see the Auto-Configuration Report, which we already learned in the first recipe of this chapter, we will see that our autoconfiguration is in the Positive Matches group, as follows:
DbCountAutoConfiguration#dbCountRunner
      - @ConditionalOnMissingBean (types: com.example.bookpubstarter.dbcount.DbCountRunner; SearchStrategy: all) found no beans (OnBeanCondition)
  1. Let's explicitly/manually create an instance of DbCountRunner in our main BookPubApplication configuration class, and we will also override its run(...) method, just so we can see the difference in the logs:
protected final Log logger = LogFactory.getLog(getClass()); 
@Bean 
public DbCountRunner dbCountRunner
(Collection<CrudRepository> repositories) { return new DbCountRunner(repositories) { @Override public void run(String... args) throws Exception { logger.info("Manually Declared DbCountRunner"); } }; }
  1. Start the application by running DEBUG=true ./gradlew clean bootRun.
  2. If we look at the console logs, we will see two things: the Auto-Configuration Report will print our autoconfiguration in the Negative Matches group and, instead of the count output for each repository, we will see Manually Declared DbCountRunner text to appear:
DbCountAutoConfiguration#dbCountRunner
      - @ConditionalOnMissingBean (types: com.example.bookpubstarter.dbcount.DbCountRunner; SearchStrategy: all) found the following [dbCountRunner] (OnBeanCondition)
    
2017-12-16 INFO com.example.bookpub.BookPubApplication$1    : Manually Declared DbCountRunner
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.9.115