【问题标题】:@cacheable not working with spring boot. Using ehcache3@cacheable 不适用于 Spring Boot。使用 ehcache3
【发布时间】:2019-06-26 10:53:52
【问题描述】:

*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 启动启动,它显示缓存已添加到缓存管理器。

【问题讨论】:

标签: java spring spring-boot ehcache-3


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-13
    • 2018-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-04
    • 2023-03-26
    • 2019-02-24
    相关资源
    最近更新 更多