java cache

配置缓存:spring-config-cache.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:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">

<cache:annotation-driven />

<bean id="accountServiceBean" class="cn.com.cis.mi.jinhua.web.controller.DemoController"/>

<!-- generic cache manager -->
<bean id="cacheManager"
class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="default" />
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="demoCache" />
</set>
</property>
</bean>
</beans>

Controller缓存简单运用:

import cn.com.cis.mi.common.service.ServiceResult;
import cn.com.cis.mi.jinhua.dataObjects.Demo;
import cn.com.cis.mi.jinhua.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class DemoController {

    @Autowired
    private DemoService demoService;

    @Cacheable(value="demoCache")
    @RequestMapping(value = "/demos", method = RequestMethod.GET)
    @ResponseBody
    public Object getDemos() {
        ServiceResult<Demo> demos = demoService.GetDemos();
        logger.info("demo日志记录!");
        return demos;
    }
}

  

posted @ 2017-07-12 16:22  hezl  阅读(187)  评论(0)    收藏  举报