[英]Spring Boot does not initialize 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 \\
每次執行的日志記錄設置都相同。
為什么會發生?
彈簧啟動模塊:
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>
application.properties:
logging.level.org.springframework.web: DEBUG
logging.level.org.hibernate: INFO
log4j.properties:
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>
每次執行的日志設置都相同
輸出的不同格式告訴我事實並非如此。
默認情況下,單元測試將使用DEBUG
作為根日志級別,在啟動Spring Boot應用程序時,默認日志級別為INFO以及您在application.properties
進行的任何自定義。 例如,由於您的配置,您可以從org.springframework.web
看到DEBUG消息。
如果要在啟動應用程序時查看來自EhCache的調試消息,請更新application.properties
:
logging.level.org.springframework.web: DEBUG
logging.level.org.hibernate: INFO
logging.level.net.sf.ehcache: DEBUG
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.