javaspringschema

What Spring class is responsible for executing schema.sql?


I am making my own ORM and I'm at the point where I need to push the SQL code I've generated from my entity classes to the database. I do not aim to copy how Spring does it but rather see what phase of the lifecycle it runs in and how exactly.

Thanks


Solution

  • The actual class that read schema.sql and execute it used to be DataSourceInitializer#createSchema(). As of Spring Boot 3.3, it is in SettingsCreator.

    Here are the high level flow which somehow triggers it :

    1. If DataSource class is found from the class-path , spring-boot auto-configuration will enable DataSourceAutoConfiguration
    2. DataSourceAutoConfiguration imports DataSourceInitializationConfiguration
    3. DataSourceInitializationConfiguration registers DataSourceInitializerPostProcessor which will be executed and force initialising DataSourceInitializerInvoker.
    4. DataSourceInitializerInvoker 's afterPropertiesSet will then execute DataSourceInitializer#createSchema() to read and execute schema.sql