有没有办法禁止在 Spring 配置中传递给定的属性值(例如 application.proeprties)。
例如,如果我们想在使用 hibernate 时防止意外的数据库架构迁移。
假设我们只想使用属性 spring.jpa.hibernate.ddl-auto
的值 validate
。如果该值不是validate
,则抛出异常或将其覆盖为validate
。
这可能吗?也许有一些“钩子(Hook)”或预处理器,可以在应用程序启动之前检查属性。
最佳答案
您可以使用EnvironmentPostProcessor
来查看环境并做任何您喜欢的事情。 This section of the documentation解释了如何做到这一点。
It is also possible to customize the Environment before the application context is refreshed using EnvironmentPostProcessor. Each implementation should be registered in META-INF/spring.factories:
org.springframework.boot.env.EnvironmentPostProcessor=com.example.YourEnvironmentPostProcessor
关于java - Spring Boot 禁止属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48183891/