无法创建请求的服务[org.hibernate.cache.spi].在Spring引导中使用Hibernate缓存.&



我正在尝试在Spring dao层休眠缓存。当我添加ehcache时,我得到一个错误依赖。

application.properties

spring.jpa.properties.hibernate.cache.use_second_level_cache=true
spring.jpa.properties.hibernate.cache.use_query_cache=true
spring.jpa.properties.javax.persistence.sharedCache.mode=ENABLE_SELECTIVE

pom.xml

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.7.4</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jcache</artifactId>
<version>5.6.0.Final</version>
</dependency>

控制台错误:

org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'delegatingApplicationListener': Singleton bean creation not allowed while singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:220) ~[spring-beans-5.3.23.jar:5.3.23]
at com.example.application.AuthMain.main(AuthMain.java:20) ~[classes/:?]
2023-04-02 14:17  INFO   38338 --- [           main] o.a.c.c.StandardService                  : Stopping service [Tomcat]
2023-04-02 14:17  INFO   38338 --- [           main] ConditionEvaluationReportLoggingListener : 
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2023-04-02 14:17 ERROR   38338 --- [           main] o.s.b.SpringApplication                  : Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration': Unsatisfied dependency expressed through method 'setConfigurers' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'openEntityManagerInViewInterceptorConfigurer' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/JpaBaseConfiguration$JpaWebConfiguration.class]: Unsatisfied dependency expressed through method 'openEntityManagerInViewInterceptorConfigurer' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'openEntityManagerInViewInterceptor' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/JpaBaseConfiguration$JpaWebConfiguration.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.cache.spi.CacheImplementor]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.resolveMethodArguments(AutowiredAnnotationBeanPostProcessor.java:767) ~[spring-beans-5.3.23.jar:5.3.23]org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119) ~[spring-beans-5.3.23.jar:5.3.23]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.3.23.jar:5.3.23]

如果我添加ehcache依赖项下面,错误就会消失。我怎么能只使用jcache没有ehcache吗?

<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.9.10</version>
</dependency>

您可以尝试在您的pom中排除ehcache。

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jcache</artifactId>
<version>5.6.0.Final</version>
<exclusions>
<exclusion>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
</exclusion>
</exclusions>
</dependency>

JCache (JSR-107)只是一个服务提供者接口(SPI)定义了一个API缓存提供者(如Ehcache或Hazelcaset等)实现SPI。

JCache本身并不是一个缓存提供者,也没有真正的缓存功能。你需要插件一个实际的实现,如咖啡因,Ehcache, Hazelcast等。

当然,hibernate-jCache只是Hibernate使用JCache API和实现缓存提供程序之间的集成和适配器层,作为Hibernate中的第二级缓存。

使用JCache可以让你选择插件任何兼容的缓存提供商,而里程(也就是能力)因缓存提供商而异。有些是简单的内存存储,而另一些是成熟的内存数据网格(imdg),具有类似数据库的功能(例如持久化,事务,函数等)。

希望这有助于澄清JCache在这里的位置。

相关内容

  • 没有找到相关文章

最新更新