这里假定我们已经有了一些现成的类和接口,比如说
1> 一个现成的User POJO对象
2> 一个UserDao接口和UserDaoHibernateImpl实现类
3> 一个UserService接口和UserServiceImpl实现类
1. 这是使用的是Maven做的项目管理工具,在使用cache(这里使用的是ehcache)之前,需要引入以下依赖
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
- <version>1.5.8</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-jdk14</artifactId>
- <version>1.5.8</version>
- </dependency>
- <dependency>
- <groupId>net.sf.ehcache</groupId>
- <artifactId>ehcache</artifactId>
- <version>2.2.0</version>
- </dependency>
- <dependency>
- <groupId>org.springmodules</groupId>
- <artifactId>spring-modules-cache</artifactId>
- <version>0.8</version>
- </dependency>
2.ehcache配置文件ehcache.xml
- <ehcache>
- <defaultCache
- maxElementsInMemory="500"
- eternal="true"
- overflowToDisk="false"
- memoryStoreEvictionPolicy="LFU"/>
- <cachename="userCache"
- maxElementsInMemory="500"
- eternal="true"
- overflowToDisk="false"
- memoryStoreEvictionPolicy="LFU"/>
- </ehcache>
3. spring配置文件
- <?xmlversion="1.0"encoding="UTF-8"?>
- <beansxmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:ehcache="http://www.springmodules.org/schema/ehcache"
- xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd
- http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsd
- http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd
- http://www.springmodules.org/schema/ehcachehttp://www.springmodules.org/schema/cache/springmodules-ehcache.xsd"
- default-lazy-init="true">
- <ehcache:configconfigLocation="classpath:test/ehcache.xml"/>
- <ehcache:proxyid="cachedUserService"refId="userService">
- <ehcache:cachingmethodName="find*"cacheName="userCache"/>
- <ehcache:cachingmethodName="list*"cacheName="userCache"/>
- <ehcache:flushingmethodName="add*"cacheNames="userCache"when="before"/>
- <ehcache:flushingmethodName="update*"cacheNames="userCache"when="before"/>
- <ehcache:flushingmethodName="delete*"cacheNames="userCache"when="before"/>
- </ehcache:proxy>
- <beanid="userService"class="test.service.UserServiceImpl">
- <propertyname="userDao"ref="userDao"/>
- </bean>
- <beanid="userDao"class="test.dao.UserDaoHibernateImpl">
- <propertyname="sessionFactory"ref="mySessionFactory"/>
- </bean>
- <tx:adviceid="txAdvice"transaction-manager="myTransactionManager">
- <tx:attributes>
- <tx:methodname="get*"propagation="SUPPORTS"read-only="true"/>
- <tx:methodname="list*"propagation="SUPPORTS"read-only="true"/>
- <tx:methodname="find*"propagation="SUPPORTS"read-only="true"/>
- <tx:methodname="*"propagation="REQUIRED"/>
- </tx:attributes>
- </tx:advice>
- <aop:config>
- <aop:pointcutid="userServiceOperation"expression="execution(**..service.UserService.*(..))"/>
- <aop:advisoradvice-ref="txAdvice"pointcut-ref="userServiceOperation"/>
- </aop:config>
- <beanid="myTransactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <propertyname="sessionFactory"ref="mySessionFactory"/>
- </bean>
- <beanid="mySessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <propertyname="dataSource"ref="myDataSource"/>
- <propertyname="mappingResources">
- <list>
- <value>test/user.hbm.xml</value>
- </list>
- </property>
- <propertyname="hibernateProperties">
- <value>hibernate.dialect=org.hibernate.dialect.HSQLDialect</value>
- </property>
- </bean>
- <beanid="myDataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close">
- <propertyname="driverClassName"value="org.hsqldb.jdbcDriver"/>
- <propertyname="url"value="jdbc:hsqldb:res:hsqldb/testdb"/>
- <propertyname="username"value="sa"/>
- <propertyname="password"value=""/>
- </bean>
- </beans>
这里有几个地方需要注意:
1> 命名空间ehcache
xmlns:ehcache="http://www.springmodules.org/schema/ehcache"
http://www.springmodules.org/schema/ehcache http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd
2> 以下ehcache的配置项必须放在第一行,否则会抛出异常Unexpected exception parsing XML document from class path resource [org/garbagecan/springstudy/dao/hibernate/spring-cache.xml]; nested exception is java.lang.IllegalStateException: An implementation of CacheProviderFacade should be registered under the name 'cacheProvider'
- <ehcache:configconfigLocation="classpath:test/ehcache.xml"/>
3> 添加了一个cachedUserService bean,直接引用真实的service bean,这里为了测试的方便,所以没有使用使用内嵌bean的定义
- <ehcache:proxyid="cachedUserService"refId="userService">
- <ehcache:cachingmethodName="find*"cacheName="userCache"/>
- <ehcache:cachingmethodName="list*"cacheName="userCache"/>
- <ehcache:flushingmethodName="add*"cacheNames="userCache"when="before"/>
- <ehcache:flushingmethodName="update*"cacheNames="userCache"when="before"/>
- <ehcache:flushingmethodName="delete*"cacheNames="userCache"when="before"/>
- </ehcache:proxy>
4. 测试Test类
- packagetest;
- importjava.util.List;
- importorg.garbagecan.springstudy.dao.hibernate.service.UserService;
- importorg.springframework.context.ApplicationContext;
- importorg.springframework.context.support.ClassPathXmlApplicationContext;
- publicclassTest{
- publicstaticvoidmain(String[]args)throwsException{
- ApplicationContextctx=newClassPathXmlApplicationContext(
- "/test/spring-cache.xml");
- UserServiceuserService=(UserService)ctx.getBean("userService");
- UserServicecachedUserService=(UserService)ctx.getBean("cachedUserService");
- for(inti=0;i<10;i++){
- Useruser=newUser();
- user.setId(""+System.currentTimeMillis()+i);
- user.setName("name_"+System.currentTimeMillis()+i);
- user.setPassword("password_"+i);
- cachedUserService.add(user);
- }
- System.out.println(cachedUserService.list());
- List<User>users=userService.list();
- for(Useruser:users){
- user.setPassword("123456");
- userService.update(user);
- }
- System.out.println(cachedUserService.list());
- for(Useruser:users){
- user.setPassword("123456");
- cachedUserService.update(user);
- }
- System.out.println(cachedUserService.list());
- }
- }