java - @cacheable 不适用于 Spring Boot。使用ehcache3

标签 java spring spring-boot ehcache-3

*Ehcache3 不适用于 Spring Boot - 我尝试了下面给出的方法。 Spring boot从不缓存组件中提到的值。无论缓存是否启用,它都会被调用n次。在日志中,它显示缓存已添加到缓存管理器,但此处情况并非如此

ehcache.xml

<ehcache:config>
  <ehcache:cache-template name="myDefaultTemplate">
    <ehcache:expiry>
      <ehcache:none/>
    </ehcache:expiry> 
  </ehcache:cache-template>

  <ehcache:cache alias="customer" uses-template="myDefaultTemplate">
     <ehcache:key-type>java.lang.Long</ehcache:key-type>
     <ehcache:value-type>com.controller.Customer</ehcache:value-type>
     <ehcache:expiry>
       <ehcache:tti unit="seconds">30</ehcache:tti>
     </ehcache:expiry>

     <ehcache:heap unit="entries">200</ehcache:heap>
  </ehcache:cache>
</ehcache:config>

在我的 pom.xml 中,我有以下配置 -

     <?xml version="1.0" encoding="UTF-8"?>
      <project xmlns="http://maven.apache.org/POM/4.0.0" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
       http://maven.apache.org/xsd/maven-4.0.0.xsd">
       <modelVersion>4.0.0</modelVersion>
       <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> 
       </parent>
       <groupId>com.test</groupId>
       <artifactId>test</artifactId>
       <version>0.0.1-SNAPSHOT</version>
      <name>test</name>
       <description>Demo project for Spring Boot</description>
       <properties>
        <java.version>1.8</java.version>
       </properties>
       <dependencies>    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
        <dependency>
            <groupId>org.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>3.6.2</version>
        </dependency>
        <dependency>
            <groupId>javax.cache</groupId>
            <artifactId>cache-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
     </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
     </project>

Application.java 启动 Spring Boot 应用程序

@SpringBootApplication
@ComponentScan(basePackages = {"com.service"})
@EnableCaching
public class TestApplication {
  public static void main(String[] args) {
    SpringApplication.run(TestApplication.class, args);
  }
}

用于缓存的组件类 -

@Component
public class CustomerService {

   @Cacheable(cacheNames = "customer",key="#id")
   public Customer getCustomer(final Long id){
     System.out.println("Returning customer information for customer id 
     {} 
   "+id);
    Customer customer = new Customer();
    customer.setCustomerId(id);
    customer.setFirstName("Test");
    customer.setEmail("contact-us@test.com");
    return  customer;
   }
}

我尝试了几种方法,在应用程序中添加组件扫描 但没有成功。

Spring boot 启动,显示缓存已添加到缓存管理器中。

最佳答案

我通过从@Component更改为@Service来让它工作。我不明白为什么缓存在组件下不起作用而在服务层中起作用

关于java - @cacheable 不适用于 Spring Boot。使用ehcache3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56771161/

相关文章:

java - 打开 Selenium 浏览器的方法不与我当前的浏览器重叠

java - 具有 stomp 安全性的 Spring websocket - 每个用户都可以订阅任何其他用户队列?

java - Spring 启动 : How to override default properties in Unit tests

java - 当我可以 ping IP 地址时,为什么 InetAddress.isReachable 返回 false?

java - 来自另一个类的多个 Activity 的 setContentView

json - 无法写入 JSON : Infinite recursion (StackOverflowError); nested exception spring boot

java - Spring 是否允许替换引用 bean 中的属性?

java - 记录需要 ASM8

java - Android WebView 中的 WebRTC 使用 NextRTC Spring Signaling Server

java - com.sun.jersey.api.client.ClientHandlerException : java.net.ConnectException:连接被拒绝:在 Spring Boot 中连接