项目已经进行一段了,今天要给项目添加hibernate的二级缓存,在hibernate3.6.10的文件夹下没有找到ehcache的jar包,就没有导入任何jar包。
首先在Spring的配置文件添加这样的配置信息:
<bean id="sessionFactory"上面这段配置文件的后三个property是新添加的。同时添加了ehcache.xml配置文件:
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 指定hibernate的配置文件位置 -->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<!-- 打开hibernate二级缓存 -->
<property name="hibernate.cache.use_second_level_cache" value="true"></property>
<!-- 指定hibernate的二级缓存 -->
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider"></property>
<!-- 启动"查询缓存" -->
<property name="hibernate.cache.use_query_cache" value="true"></property>
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<diskStore path="d:/ehcache"></diskStore>
<defaultCache maxElementsInMemory="1000" eternal="false"
timeToIdleSeconds="120" timeToLiveSeconds="180" overflowToDisk="true"></defaultCache>
</ehcache>
在其中一个*.hbm.xml添加<cache usage="read-only"/>,然后开始测试,结果报错了,错误信息如下:
18:09:58,419 ERROR ContextLoader:215 - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'moduleServiceImpl': Injection of resource fields failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in file [D:\develop\ZYPT\.metadata\.me_tcat\webapps\zypt\WEB-INF\classes\applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'hibernate.cache.use_second_level_cache' of bean class [org.springframework.orm.hibernate3.LocalSessionFactoryBean]: Nested property in path 'hibernate.cache.use_second_level_cache' does not exist; nested exception is org.springframework.beans.NotReadablePropertyException: Invalid property 'hibernate' of bean class [org.springframework.orm.hibernate3.LocalSessionFactoryBean]: Bean property 'hibernate' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessAfterInstantiation(CommonAnnotationBeanPostProcessor.java:292)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:959)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3827)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4334)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:920)
at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:883)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:492)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1138)