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

时间:2019-06-26 10:53:52

标签: 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>

启动Spring Boot应用程序的Application.java

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

1 个答案:

答案 0 :(得分:0)

我通过将@Component更改为@Service使其工作。我不明白为什么缓存不能在组件下工作并且不能在服务层工作