robert_bor
robert_bor

Reputation: 125

@AutoConfigureBefore does not trigger

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:

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:

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.

Upvotes: 6

Views: 9463

Answers (1)

Stephane Nicoll
Stephane Nicoll

Reputation: 33091

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.

Upvotes: 10

Related Questions