繁体   English   中英

为什么是 spring.jpa.properties。 Spring 引导属性中并不总是需要前缀

[英]Why spring.jpa.properties. prefix is not always required in Spring Boot properties

我学习 Spring 引导,发现在某些示例中使用前缀spring.jpa.properties的相同属性,而其他示例不使用前缀

例如:文章解释了二级缓存https://www.baeldung.com/hibernate-second-level-cache和 autor 显示了所需属性的示例(在 autor 的 gitHub 上的示例):

hibernate.cache.use_second_level_cache=true
hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory

但这对我不起作用,我花了几个小时寻找原因,但后来我注意到,有人使用前缀spring.jpa.properties. 让它工作(视频课的确切时刻):

spring.jpa.properties.hibernate.cache.use_second_level_cache=true
spring.jpa.properties.hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory

这对我来说非常有用。

所以我的问题是:

  • 为什么他们以不同的方式工作?
  • 如何理解哪些属性在哪些情况下需要这个前缀?
  • 它可能与我的项目设置或 Spring 引导版本有关吗?
  • 任何其他建议表示赞赏:)

先感谢您!

这里有几件事在起作用。 首先,您使用的教程是使用普通的 Spring 而不是 Spring 引导。 这意味着手动配置的EntityManagerFactory可以直接在其上设置提供者特定的属性。 hibernate.cache.use_second_level_cache

当您决定使用 Spring Boot 时,您没有手动配置的EntityManagerFactory (尽管您可以。)。 这意味着您正在使用自动配置的EntityManagerFactory 此属性位于属性的spring.jpa命名空间中。

As it isn't feasible to specify properties for each and every extension for Hibernate (or other JPA providers) only some commonly used ones are exposed like spring.jpa.generate-ddl and a few provider specific ones in the spring.jpa.hibernate命名空间。 但是,要以自动配置的方式设置所有其他属性,则需要其他内容。 因此spring.jpa.properties前缀。 在那里指定的任何内容都将按原样(当然没有上述前缀)传递给EntityManagerFactory进行配置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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