简体   繁体   中英

Spring Boot: How to Deactivate All Caches for JPA?

I have a spring boot application where the main class is annotated with @SpringBootApplication .

I am connected to a MySQL database.

I want to deactivate all JPA/Hibernate based caching.

Presently, I can find a lot of auto configured caching enabled - according to the startup messages. How can I turn the caching off?

Here is an excerpt of the startup messages - filtered just for the eyecatcher cach :

Positive matches:
-----------------
   GenericCacheConfiguration matched
      - Automatic cache type (CacheCondition)

   NoOpCacheConfiguration matched
      - Automatic cache type (CacheCondition)

   RedisCacheConfiguration matched
      - Automatic cache type (CacheCondition)

   SimpleCacheConfiguration matched
      - Automatic cache type (CacheCondition)

If you don't want those configurations to be auto-configured by Spring Boot, this is where you'd use the @EnableAutoConfiguration annotation to accomplish this.

@EnableAutoConfiguration(exclude={
  GenericCacheConfiguration.class,
  NoOpCacheConfiguration.class,
  RedisCacheConfiguration.class,
  SimpleCacheConfiguration.class
})

Note however that even if parts of your application require and use any cache subsystem provided by these auto configurations listed above, that has no influence on Hibernate.

From the spring documentation, Spring will not specifically enable any Hibernate 2LC functionality when any cache provider exists on the classpath. So this means that the default behavior for 2LC in Hibernate remains unchanged, eg you must specifically enable it yourself to use it .

No worries, the configuration classes GenericCacheConfiguration , NoOpCacheConfiguration , RedisCacheConfiguration and SimpleCacheConfiguration are not actually activated. The debug info you see is a false positive.

They were not activated because their root auto-configuration class CacheAutoConfigure (which referenced them) did not match its condition. In fact, GenericCacheConfiguration , RedisCacheConfiguration , SimpleCacheConfiguration would not match a second round of matching for bean presence ( ConfigurationPhase.REGISTER_BEAN ) even if their root auto-configuration class would have actually been activated.

If you still insist on total exclusion of the above mentioned classes, indeed you cannot exclude them directly, because they are package-local (not public ). The solution is to exclude their root auto-configuration class:

@EnableAutoConfiguration(exclude=CacheAutoConfigure.class)

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