【问题标题】:Ehcache not getting initialized in spring bootEhcache 未在 Spring Boot 中初始化
【发布时间】:2018-05-29 00:52:04
【问题描述】:

我正在使用 Spring 启动、数据、JPA 和休眠。 hibernate eh-cache 不适用于被命中两次或更多的同一查询。

看到以下错误:“2017-12-14 14:34:15.223 DEBUG 15548 --- [ost-startStop-1] n.sf.ehcache.config.ConfigurationHelper : CacheDecoratorFactory 未配置为 defaultCache。跳过' com.bestbuy.integration.entity.InfrastructureMapping'。”

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>

    <groupId>com.bestbuy.integration</groupId>
    <artifactId>build-admin-operation</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <profiles>
        <profile>
            <id>development</id>
            <properties>
                <spring.profiles.active>development</spring.profiles.active>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
    </profiles>

    <name>build-admin-operation</name>
    <description>populate dropdowns and submits the created job in Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.BUILD-SNAPSHOT</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.BUILD-SNAPSHOT</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</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-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
        </dependency>
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc7</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-ehcache</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
                <version>2.0.0.M7</version>
            </dependency>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-validator</artifactId>
                <version>5.3.6.Final</version>
            </dependency>
            <dependency>
                <groupId>com.oracle</groupId>
                <artifactId>ojdbc7</artifactId>
                <version>12.1.0.2</version>
            </dependency>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-ehcache</artifactId>
                <version>5.2.12.Final</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>


</project>

实体:

@Entity
@Cacheable
@Table(name="INFRASTRUCTURE_MAPPING")
public class InfrastructureMapping {

    @Id
    @Column(name="ID")
    private String mappingId;
    @Column(name="JAVA_HOME_PATH")
    private String JAVAHomePath;
    @Column(name="INSTALLATION_HOME_PATH")
    private String InstallationHomePath;
    @Column(name="MASTER_SLAVE")
    private String MasterSlave;
    @Column(name="NUMBER_OF_PROFILES")
    private String NumberofProfiles;
    @Column(name="PROFILE_NAME")

应用程序属性:

EhCache

spring.jpa.properties.hibernate.cache.use_second_level_cache=true
spring.jpa.properties.hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
spring.jpa.properties.javax.persistence.sharedCache.mode=ENABLE_SELECTIVE
logging.level.net.sf.ehcache=debug

【问题讨论】:

    标签: hibernate jpa spring-boot ehcache


    【解决方案1】:

    运行这个来检查你的缓存是否工作。

    import net.sf.ehcache.CacheManager;
    import net.sf.ehcache.Status;
    import net.sf.ehcache.statistics.StatisticsGateway;
    
    String[] cacheNames = CacheManager.ALL_CACHE_MANAGERS.get(0).getCacheNames();
    int size = CacheManager.ALL_CACHE_MANAGERS.get(0).getCache(<EntityName>).getSize();
    String name = CacheManager.ALL_CACHE_MANAGERS.get(0).getCache(<EntityName>).getName();
    List keys = CacheManager.ALL_CACHE_MANAGERS.get(0).getCache(<EntityName>).getKeys();
    Status status = CacheManager.ALL_CACHE_MANAGERS.get(0).getCache(<EntityName>).getStatus();
    
    logger.info(cacheNames);
    logger.info(size + "");
    logger.info(name + "");
    logger.info("{}", keys);
    logger.info("{}", status);
    

    【讨论】:

    • 请告诉我这个sn-p的结果。
    猜你喜欢
    • 1970-01-01
    • 2020-12-14
    • 2015-07-29
    • 2018-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-21
    • 2018-07-08
    相关资源
    最近更新 更多