【问题标题】:Validate "spring.jpa.hibernate.ddl-auto" during springboot startup在springboot启动期间验证“spring.jpa.hibernate.ddl-auto”
【发布时间】:2018-11-13 08:33:08
【问题描述】:

有没有办法在应用程序启动期间验证“spring.jpa.hibernate.ddl-auto”属性以确保它只设置为无?我想强制所有部署(包括开发)使用 liquibase。

编辑:- 我还需要确保在生产中不会意外设置此属性,这可能会清除数据。

【问题讨论】:

    标签: java hibernate spring-boot jpa


    【解决方案1】:

    作为最佳实践,您可以维护一个通用的 application.properties/yml 文件并在其中设置属性 (spring.jpa.hibernate.ddl-auto)。之后,维护一个单独的 property/yml 文件 (application_*.properties/yml),默认情况下,它将从 application.properties/yml 文件中获取属性。 此外,您可以在父文件中维护其他“通用”属性。

    【讨论】:

    • 我的要求也是确保有人不小心在生产中设置了属性。这种方法仍然可能发生。
    【解决方案2】:

    您可以通过实现ApplicationListener<ContextRefreshedEvent> 类来启动您的应用程序,例如:

    @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();
    
        }
    }
    

    此外,您甚至可以通过注册自己的FailureAnalyzer. 来使其更详细

    【讨论】:

    • 这种方法的问题是只有在应用程序初始化之后才进行验证。当我们到达这里时,损坏(即重新创建表)已经完成。
    猜你喜欢
    • 2021-03-13
    • 2018-09-16
    • 2018-04-11
    • 2022-08-11
    • 1970-01-01
    • 2021-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多