spring 3.x整合ehcache 2.x

来源:互联网 发布:simnow python 编辑:程序博客网 时间:2024/06/11 07:30

1.pom.xml

        <!-- spring 相关的jar-->        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context-support</artifactId>            <version>${spring-version}</version>        </dependency>        <!-- https://mvnrepository.com/artifact/com.googlecode.ehcache-spring-annotations/ehcache-spring-annotations -->        <dependency>            <groupId>com.googlecode.ehcache-spring-annotations</groupId>            <artifactId>ehcache-spring-annotations</artifactId>            <version>1.2.0</version>        </dependency>        <!-- https://mvnrepository.com/artifact/net.sf.ehcache/ehcache -->        <dependency>            <groupId>net.sf.ehcache</groupId>            <artifactId>ehcache</artifactId>            <version>2.10.2</version>        </dependency>

2.applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:aop="http://www.springframework.org/schema/aop"    xmlns:c="http://www.springframework.org/schema/c"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:jee="http://www.springframework.org/schema/jee"    xmlns:p="http://www.springframework.org/schema/p"    xmlns:util="http://www.springframework.org/schema/util"    xmlns:cache="http://www.springframework.org/schema/cache"     xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd         http://www.springframework.org/schema/cache  http://www.springframework.org/schema/cache/spring-cache-3.2.xsd          http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.2.xsd        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">    <!--other etc-->    <!-- 缓存配置 -->      <ehcache:annotation-driven />     <ehcache:config cache-manager="cacheManager">         <ehcache:evict-expired-elements interval="60" />     </ehcache:config>     <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">         <property name="configLocation" value="classpath:ehcache.xml"/>     </bean></beans>

3.ehcache.xml

<?xml version="1.0" encoding="UTF-8"?><ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:noNamespaceSchemaLocation="ehcache.xsd"     updateCheck="true"    monitoring="autodetect"     dynamicConfig="true">    <diskStore path="java.io.tmpdir"/>      <defaultCache             maxElementsInMemory="1000"             eternal="false"             timeToIdleSeconds="120"             timeToLiveSeconds="120"             overflowToDisk="false"/>      <cache name="myCache"             maxElementsOnDisk="20000"             maxElementsInMemory="2000"             eternal="true"             overflowToDisk="true"             diskPersistent="true"/>  </ehcache> 

4.代码:

import com.googlecode.ehcache.annotations.Cacheable;@Servicepublic class ChannelServiceImple implements ChannelService {    @Cacheable(cacheName="myCache")    @Override    public List<ChannelCategory> getChannelCategories() {//Q5        // TODO Auto-generated method stub        List<ChannelCategory> ccs=Collections.emptyList();        log.info("[apobates][cache start]service:getChannelCategories");        //ETC        log.info("[apobates][cache finish]service:getChannelCategories");        return ccs;    }}

5.看看log日志,缓存是否起作用了 ,
之前(没有ehcache):

 [ INFO ]  [apobates]user channel category [ INFO ]  [apobates][cache start]service:getUserChannelCategories@by:1 [ INFO ]  [apobates][cache finish]service:getUserChannelCategories@by:1 [ INFO ]  [apobates]global channel category [ INFO ]  [apobates][cache start]service:getChannelCategories [ INFO ]  [apobates][cache finish]service:getChannelCategories [ INFO ]  [apobates]user channel category [ INFO ]  [apobates][cache start]service:getUserChannelCategories@by:1 [ INFO ]  [apobates][cache finish]service:getUserChannelCategories@by:1 [ INFO ]  [apobates]global channel category [ INFO ]  [apobates][cache start]service:getChannelCategories [ INFO ]  [apobates][cache finish]service:getChannelCategories [ INFO ]  [apobates]user channel category [ INFO ]  [apobates][cache start]service:getUserChannelCategories@by:1 [ INFO ]  [apobates][cache finish]service:getUserChannelCategories@by:1 [ INFO ]  [apobates]global channel category [ INFO ]  [apobates][cache start]service:getChannelCategories [ INFO ]  [apobates][cache finish]service:getChannelCategories [ INFO ]  [apobates]user channel category [ INFO ]  [apobates][cache start]service:getUserChannelCategories@by:1 [ INFO ]  [apobates][cache finish]service:getUserChannelCategories@by:1 [ INFO ]  [apobates]global channel category [ INFO ]  [apobates][cache start]service:getChannelCategories [ INFO ]  [apobates][cache finish]service:getChannelCategories

global channel category是spring mvc controller输出的

@Controller@Scope("prototype")@RequestMapping(value="/channel")@SessionAttributes({"channelForm"})public class ChannelController {    @Autowired    private ChannelService channelService;    private static final Logger log=Logger.getLogger(ChannelController.class);    //GLcategory    @ModelAttribute    public void addInitAttr(Model model,HttpServletRequest request){        log.info("[apobates]global channel category");        model.addAttribute("GLcategory",channelService.getChannelCategories());    }

当前():

 [ INFO ]  FrameworkServlet 'apobates_client': initialization completed in 213 ms [ INFO ]  [apobates]user channel category [ INFO ]  [apobates][cache start]service:getUserChannelCategories@by:1 [ INFO ]  [apobates][cache finish]service:getUserChannelCategories@by:1 [ INFO ]  [apobates]global channel category [ INFO ]  [apobates][cache start]service:getChannelCategories [ INFO ]  [apobates][cache finish]service:getChannelCategories [ INFO ]  [apobates]user channel category [ INFO ]  [apobates]global channel category [ INFO ]  [apobates]user channel category [ INFO ]  [apobates]global channel category [ INFO ]  [apobates]user channel category [ INFO ]  [apobates]global channel category [ INFO ]  [apobates]user channel category [ INFO ]  [apobates]global channel category [ INFO ]  [apobates]user channel category [ INFO ]  [apobates][cache start]service:getUserChannelCategories@by:19 [ INFO ]  [apobates][cache finish]service:getUserChannelCategories@by:19 [ INFO ]  [apobates]global channel category
0 0
原创粉丝点击