简体   繁体   中英

Validate “spring.jpa.hibernate.ddl-auto” during springboot startup

Is there a way to validate the "spring.jpa.hibernate.ddl-auto" property during application startup to ensure that it's set only to none? I want to force all deployments(including dev) to use liquibase.

Edit :- I also need to ensure that this property is not accidentally set in production, which could wipe out the data.

As a best practice, you can maintain a universal application.properties/yml file and set the property ( spring.jpa.hibernate.ddl-auto ) there. Afterwards, maintain a separate property/yml file ( application_*.properties/yml ) which will fetch the properties from application.properties/yml file, by default. Also, you can maintain other "common" properties in the parent file.

You can hook up on starting of your application by implementing ApplicationListener<ContextRefreshedEvent> class, like:

@Component
public class YourListner implements ApplicationListener<ContextRefreshedEvent> {

    @Value("${spring.jpa.properties.hibernate.ddl-auto}")
    private String hibernateDdlAuto;


    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {

        if (!"none".equalsIgnoreCase(hibernateDdlAuto))
            throw new MyValidationException();

    }
}

Moreover, you can even make it more verbose by registering your own FailureAnalyzer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM