java - Spring Boot没有初始化Ehcache

标签 java spring spring-mvc spring-boot ehcache

我正在使用 Spring Boot 和 Ehcache 开发一个项目。

当我使用 Junit 执行该项目的一个方法时,我看到 Ehcache 已完全初始化。

20:08:27.346 [main] INFO org.springframework.cache.ehcache.EhCacheManagerFactoryBean - Initializing EhCache CacheManager
20:08:27.348 [main] DEBUG net.sf.ehcache.config.ConfigurationFactory - Configuring ehcache from InputStream
20:08:27.361 [main] DEBUG net.sf.ehcache.config.BeanHandler - Ignoring ehcache attribute xmlns:xsi
20:08:27.362 [main] DEBUG net.sf.ehcache.config.BeanHandler - Ignoring ehcache attribute xsi:noNamespaceSchemaLocation
20:08:27.363 [main] DEBUG net.sf.ehcache.config.DiskStoreConfiguration - Disk Store Path: C:\TEMP\
20:08:27.378 [main] DEBUG net.sf.ehcache.CacheManager - Creating new CacheManager with Configuration Object
20:08:27.379 [main] DEBUG net.sf.ehcache.util.PropertyUtil - propertiesString is null.
20:08:27.388 [main] DEBUG net.sf.ehcache.config.ConfigurationHelper - No CacheManagerEventListenerFactory class specified. Skipping...
20:08:27.437 [main] DEBUG net.sf.ehcache.Cache - No BootstrapCacheLoaderFactory class specified. Skipping...
20:08:27.437 [main] DEBUG net.sf.ehcache.Cache - CacheWriter factory not configured. Skipping...
20:08:27.437 [main] DEBUG net.sf.ehcache.config.ConfigurationHelper - No CacheExceptionHandlerFactory class specified. Skipping...
20:08:27.451 [main] DEBUG net.sf.ehcache.store.MemoryStore - Initialized net.sf.ehcache.store.MemoryStore for rrr1
20:08:27.476 [main] DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl - Mocking Pass-Through Statistic: LOCAL_OFFHEAP_SIZE
20:08:27.476 [main] DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl - Mocking Pass-Through Statistic: LOCAL_OFFHEAP_SIZE_BYTES
20:08:27.476 [main] DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl - Mocking Pass-Through Statistic: LOCAL_DISK_SIZE
20:08:27.476 [main] DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl - Mocking Pass-Through Statistic: LOCAL_DISK_SIZE_BYTES
20:08:27.477 [main] DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl - Mocking Pass-Through Statistic: WRITER_QUEUE_LENGTH
20:08:27.477 [main] DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl - Mocking Pass-Through Statistic: REMOTE_SIZE
20:08:27.477 [main] DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl - Mocking Pass-Through Statistic: LAST_REJOIN_TIMESTAMP
20:08:27.487 [main] DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl - Mocking Operation Statistic: OFFHEAP_GET
20:08:27.487 [main] DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl - Mocking Operation Statistic: OFFHEAP_PUT
20:08:27.487 [main] DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl - Mocking Operation Statistic: OFFHEAP_REMOVE
20:08:27.488 [main] DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl - Mocking Operation Statistic: DISK_GET
20:08:27.488 [main] DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl - Mocking Operation Statistic: DISK_PUT
20:08:27.488 [main] DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl - Mocking Operation Statistic: DISK_REMOVE
20:08:27.488 [main] DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl - Mocking Operation Statistic: XA_COMMIT
20:08:27.488 [main] DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl - Mocking Operation Statistic: XA_ROLLBACK
20:08:27.488 [main] DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl - Mocking Operation Statistic: XA_RECOVERY
20:08:27.489 [main] DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl - Mocking Operation Statistic: CLUSTER_EVENT
20:08:27.489 [main] DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl - Mocking Operation Statistic: NONSTOP
20:08:27.495 [main] DEBUG net.sf.ehcache.Cache - Initialised cache: rrr1
20:08:27.495 [main] DEBUG net.sf.ehcache.config.ConfigurationHelper - CacheDecoratorFactory not configured. Skipping for 'rrr1'.
20:08:27.495 [main] DEBUG net.sf.ehcache.config.ConfigurationHelper - CacheDecoratorFactory not configured for defaultCache. Skipping for 'rrr1'.

但是当我在同一个项目中使用 Spring Boot 时,我没有看到 Ehcahe 完全初始化。

2016-08-09 20:01:20.532  INFO 20200 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
    name: JpaPersistenceUnit
    ...]
2016-08-09 20:01:20.599  INFO 20200 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.1.0.Final}
2016-08-09 20:01:20.601  INFO 20200 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2016-08-09 20:01:20.602  INFO 20200 --- [           main] org.hibernate.cfg.Environment            : HHH000021: Bytecode provider name : javassist
2016-08-09 20:01:20.733  INFO 20200 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2016-08-09 20:01:21.014  INFO 20200 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect
2016-08-09 20:01:21.904  INFO 20200 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'JpaPersistenceUnit'
2016-08-09 20:01:22.185  INFO 20200 --- [           main] o.s.c.ehcache.EhCacheManagerFactoryBean  : Initializing EhCache CacheManager
2016-08-09 20:01:22.803  INFO 20200 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@515c6049: startup date [Tue Aug 09 20:01:17 NOVT 2016]; root of context hierarchy
2016-08-09 20:01:22.879 DEBUG 20200 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking for request mappings in application context: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@515c6049: startup date [Tue Aug 09 20:01:17 NOVT 2016]; root of context hierarchy
2016-08-09 20:01:22.891 DEBUG 20200 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : 1 request handler methods found on class com.ric.web.GreetingController: {public java.lang.String com.ric.web.GreetingController.greeting(java.lang.String)={[/greeting]}}
2016-08-09 20:01:22.891  INFO 20200 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/greeting]}" onto public java.lang.String com.ric.web.GreetingController.greeting(java.lang.String)
2016-08-09 20:01:22.894 DEBUG 20200 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : 2 request handler methods found on class org.springframework.boot.autoconfigure.web.BasicErrorController: {public org.springframework.http.ResponseEntity org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)={[/error]}, public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)={[/error],produces=[text/html]}}
2016-08-09 20:01:22.895  INFO 20200 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2016-08-09 20:01:22.895  INFO 20200 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2016-08-09 20:01:22.945 DEBUG 20200 --- [           main] o.s.w.s.h.BeanNameUrlHandlerMapping      :

当我使用 Spring Boot 时,我没有看到任何带有以下内容的行 统计数据:LOCAL_OFFHEAP_SIZE_BYTES 统计数据:LOCAL_DISK_SIZE 统计数据:LOCAL_DISK_SIZE_BYTES

此外,我没有看到缓存的磁盘存储路径,如第一个示例所示: DiskStoreConfiguration - 磁盘存储路径:C:\TEMP\

每次执行的日志记录设置都是相同的。

为什么会发生这种情况?

Spring 启动模块:

AppConfig.java:

@Configuration
@ComponentScan({"com.ric.bill"})
@ImportResource("spring.xml")
@EnableCaching
public class AppConfig {



}

BillWebApplication.java:

@SpringBootApplication
@EnableCaching
public class BillWebApplication {

    public static void main(String[] args) {
        SpringApplication.run(BillWebApplication.class, args);
    }
}

Ehcache.xml:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="ehcache.xsd">


    <diskStore path="java.io.tmpdir"/>

    <cache name="rrr1"
       maxElementsInMemory="500000" 
       eternal="true" 
       overflowToDisk="false" 
       memoryStoreEvictionPolicy="LRU">
    </cache>

</ehcache>

应用程序属性:

logging.level.org.springframework.web: DEBUG
logging.level.org.hibernate: INFO

log4j.属性:

log4j.rootLogger=info, console

log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

spring.xml:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd">

    <!-- *******************************
         ***** CACHE CONFIGURATION ***** 
         ******************************* -->                
        <cache:annotation-driven  cache-manager="cacheManager" />

    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
        <property name="cacheManager" ref="ehcache"/>
    </bean>
    <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation" value="classpath:ehcache.xml"/>
        <property name="shared" value="true"/>
    </bean> 

    <bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource" destroy-method="close"> 
          <property name="URL" value="jdbc:oracle:thin:@192.168.100.92:1521:DEVDB" /> 
          <property name="user" value="lev"/> 
          <property name="password" value="testtest1"/> 
          <property name="connectionCachingEnabled" value="true"/> 
    </bean>

    <bean
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
        id="entityManagerFactory">
        <property name="dataSource" ref="dataSource" />
    </bean>

     <context:component-scan base-package="com.ric.bill" />
     <context:component-scan base-package="com.ric.bill.dao.impl" />
     <context:component-scan base-package="com.ric.bill.mm.impl" />


    <bean class="org.springframework.orm.jpa.JpaTransactionManager"
        id="transactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

 <tx:annotation-driven mode="proxy" transaction-manager="transactionManager"/>

    <context:spring-configured />
    <context:annotation-config />


</beans>

最佳答案

log settings are the same for each execution

输出的不同格式告诉我事实并非如此。

默认情况下,单元测试将使用 DEBUG作为根日志记录级别,当您启动 Spring Boot 应用程序时,默认日志记录级别是 INFO 加上您在 application.properties 中进行的任何自定义设置。 。例如,您可以看到来自 org.springframework.web 的 DEBUG 消息。由于您的配置。

如果您想在启动应用程序时看到来自 EhCache 的 DEBUG 消息,请更新 application.properties :

logging.level.org.springframework.web: DEBUG
logging.level.org.hibernate: INFO
logging.level.net.sf.ehcache: DEBUG

关于java - Spring Boot没有初始化Ehcache,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38853354/

相关文章:

java - 将少量信息保存为 android 中的设置(例如第一次运行该应用程序)

java - 在哪里实例化类

java - 使用没有任何值(value)的 CDI @Named CDI 有什么意义吗?

java - HIbernate Envers @AuditMappedBy 导致 SQLException : Parameter index out of range

spring - Docker 容器访问外部 MySQL 主机

java - 在准备好的语句中获取列名而不是值

Spring @Cacheable 正在破坏 @RequestMapping

java - Spring get请求到Object映射定制方式

java - Spring 3 中的动态表单

java - Spring MVC 表单 : data binding of a list of abstract class