6

The intention of this project is to create hooks into Spring Boot's lifecycle, right before Liquibase executes its database schema changesets. The hooks will eventually be used to start / stop a Docker (or Docker-like) container with a Postgres instance. The project must be able to deal with:

  • tests run from the IDE (IntellIJ, STS)
  • tests run with Maven Surefire
  • spring-boot:run
  • executable JAR
  • integration test on CI server

Given the above limitations, the best approach appeared to be to have an Auto-Configuration and instructing it to run before Liquibase.

The Auto-Configuration class has been annotated:

@ConditionalOnProperty(prefix = "docker_42", 
    name = "enabled", matchIfMissing = false)
@AutoConfigureBefore({LiquibaseAutoConfiguration.class })
public class Docker42AutoConfiguration {

spring.factories has a single entry:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
nl._42.autoconfig.Docker42AutoConfiguration

The entire (stripped down) project can be found here: https://github.com/robert-bor/auto-configuration-question

The results can be verified by either:

  • setting up the project in your IDE & running a test, or
  • executing spring-boot:run

In Spring Boot's log you will see Liquibase executing before the custom AutoConfiguration.

Note that various other routes have been tried (ApplicationListener, RunListener), but none played nice with all the required in-roads.

Pointers as to why @AutoConfigureBefore does not work in my project would be very much appreciated.

3
  • 1
    I think you misunderstand what "before" means. It has nothing to do about something actually running before something else, it's about registering a bean definition before another configuration class has a chance to register one. Do I understand you thought you could use it to "run" something before something else? This is a configuration class, not an actual bean so that's no the same semantic. Nov 21, 2016 at 19:45
  • @AutoConfigureBefore documentation says: "Hint for that an auto-configuration should be applied before other specified auto-configuration classes." The documented effect is exactly the effect I am looking for.
    – robert_bor
    Nov 21, 2016 at 19:57
  • @stephane-nicoll, you, as a Spring Boot core committer, and given that I probably misunderstood @AutoConfigureBefore, what would you recommend for me to do given the above restrictions?
    – robert_bor
    Nov 21, 2016 at 20:47

1 Answer 1

10

You're trying to apply a configuration semantic to some runtime constraints. Auto-configuration is about providing bean definitions in the context, something that will ultimately create bean instances that you can wire in your components.

Assume that you need bean Foo in order to auto-configure something. You need to make sure that FooAutoConfiguration runs before yours so that the context has a chance to contain a bean definition for Foo. That is explained in quite details in our last Devoxx university.

The documentation you referenced in that comment does not imply runtime constraints in any way:

Hint for that an auto-configuration should be applied before other specified auto-configuration classes.

applying the auto-configuration does not mean that the beans created by that configuration will effectively start before the beans created by another configuration. In other words, what you've done is making sure that your auto-configuration configures the context before the liquibase auto-configuration has a chance to do so. It does not imply, in any way, that the beans that will be created (we're not there yet) will be created in that order.

If that's what you want to do and you have no bean dependency, you can just forget about all that and create a bean lifecycle dependency instead. It can be tricky because liquibase may or may not be there but we essentially do that for Hazelcast (see the auto-configuration). Basically we need to make sure that Hazelcast is started before the JPA container is started in case it wants to use Hazelcast as second level cache.

4
  • Thanks, Stephane. The video was informative. Reminds me to reserve a spot to Devoxx next year. The Hazelcast example indeed shows the way. Early results are promising.
    – robert_bor
    Nov 22, 2016 at 20:23
  • the url of 'the auto-configuration' result in 404, could you plz fix it
    – mt eee
    Feb 26, 2019 at 10:24
  • Thanks for reporting it. It should be fixed. Feb 26, 2019 at 13:33
  • thanks for explaintiation, it helps me solve stackoverflow.com/q/65321761/4695118, when I need trigger action before bean created Dec 16, 2020 at 12:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.