如何在ehcache中缓存springboot

  介绍

如何在ehcache中缓存springboot ?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

springboot是什么

springboot一种全新的编程规范,其设计目的是用来简化新弹簧应用的初始搭建以及开发过程,springboot也是一个服务于框架的框架,服务范围是简化配置文件。

<强> 1。创建一个弹簧引导工程并添加Maven依赖

你所创建的弹簧引导应用程序的Maven依赖文件至少应该是下面的样子:

& lt; ? xml  version=?.0“,编码=癠TF-8" ?比;   & lt; project  xmlns=癶ttp://maven.apache.org/POM/4.0.0", xmlns: xsi=癶ttp://www.w3.org/2001/XMLSchema-instance"   ,xsi: schemaLocation=癶ttp://maven.apache.org/POM/4.0.0 , http://maven.apache.org/xsd/maven-4.0.0.xsd"比;   ,& lt; modelVersion> 4.0.0   ,& lt; parent>   ,& lt; groupId> org.springframework.boot   ,& lt; artifactId> spring-boot-starter-parent   ,& lt; version> 2.1.3.RELEASE   ,& lt; relativePath/祝辞,& lt; !——, lookup  parent 得到repository ——比;   ,& lt;/parent>   ,& lt; groupId> com.ramostear   ,& lt; artifactId> cache   ,& lt; version> 0.0.1-SNAPSHOT   ,& lt; name> cache   ,& lt; description> Demo  project  for  Spring  Boot      ,& lt; properties>   ,& lt; java.version> 1.8 & lt;/java.version>   ,& lt;/properties>      ,& lt; dependencies>   ,& lt; dependency>   & lt;才能groupId> org.springframework.boot</groupId>   & lt;才能artifactId> spring-boot-starter-cache</artifactId>   ,& lt;/dependency>   ,& lt; dependency>   & lt;才能groupId> org.springframework.boot</groupId>   & lt;才能artifactId> spring-boot-starter-web</artifactId>   ,& lt;/dependency>   ,& lt; dependency>   & lt;才能groupId> org.ehcache</groupId>   & lt;才能artifactId> ehcache</artifactId>   ,& lt;/dependency>   ,& lt; dependency>   & lt;才能groupId> javax.cache</groupId>   & lt;才能artifactId> cache-api</artifactId>   ,& lt;/dependency>   ,& lt; dependency>   & lt;才能groupId> org.springframework.boot</groupId>   & lt;才能artifactId> spring-boot-starter-test</artifactId>   & lt;才能scope> test</scope>   ,& lt;/dependency>   ,& lt; dependency>   & lt;才能groupId> org.projectlombok</groupId>   & lt;才能artifactId> lombok</artifactId>   ,& lt;/dependency>   ,& lt;/dependencies>      ,& lt; build>   ,& lt; plugins>   & lt;才能plugin>   & lt;才能groupId> org.springframework.boot</groupId>   & lt;才能artifactId> spring-boot-maven-plugin</artifactId>   & lt;才能/plugin>   ,& lt;/plugins>   ,& lt;/build>      & lt;/project>

依赖说明:

<李>

spring-boot-starter-cache为弹簧引导应用程序提供缓存支持

<李>

ehcache提供了ehcache的缓存实现

<李>

缓存api的提供了基于jsr - 107的缓存规范

<强> 2。配置Ehcache缓存

现在,需要告诉弹簧引导去哪里找缓存配置文件,这需要在春天引导配置文件中进行设置:

spring.cache.jcache.config=类路径:Ehcache。xml

然后使用@EnableCaching注解开启弹簧引导应用程序缓存功能,你可以在应用主类中进行操作:

package  com.ramostear.cache;      import  org.springframework.boot.SpringApplication;   import  org.springframework.boot.autoconfigure.SpringBootApplication;   import  org.springframework.cache.annotation.EnableCaching;      @SpringBootApplication   @EnableCaching   public  class  CacheApplication  {      ,public  static  void  main (String [], args), {   ,SpringApplication.run (CacheApplication.class, args);   ,}   }

接下来,需要创建一个ehcache的配置文件,该文件放置在类路径下,如资源目录下:

& lt; config  xmlns: xsi=癶ttp://www.w3.org/2001/XMLSchema-instance"   ,,,xmlns=癶ttp://www.ehcache.org/v3"   ,,,xmlns: jsr107=癶ttp://www.ehcache.org/v3/jsr107"   ,,,xsi: schemaLocation=?   ,,,,,http://www.ehcache.org/v3 , http://www.ehcache.org/schema/ehcache-core-3.0.xsd   ,,,,,http://www.ehcache.org/v3/jsr107 , http://www.ehcache.org/schema/ehcache - 107 - ext - 3.0.xsd"比;   & lt;才能service>   ,,,& lt; jsr107: defaults 启用统计数据=皌rue"/比;   & lt;才能/service>      & lt;才能cache 别名=皃erson"比;   ,,,& lt; key-type> java.lang.Long

如何在ehcache中缓存springboot