java ehcache 配置_EhCache的配置

JPA和Hibernate的二级缓存都是这样做的

代码目录:

349d18b911ef0b7443349eb1d05acb79.png

这是基础的jar包,如果少的话,再去maven下载

org.springframework

spring-context

${org.springframework-version}

commons-logging

commons-logging

org.springframework

spring-webmvc

${org.springframework-version}

org.aspectj

aspectjrt

${org.aspectj-version}

junit

junit

4.11

test

net.sf.ehcache

ehcache-core

2.6.6

org.springframework

spring-test

${org.springframework-version}

ehcache.xml :

这里采用两种bean的配置方式,一种是xml(EhCacheConfig.xml),一种是java(EhCacheConfig.java),如下:

EhCacheConfig.xml:

class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">

EhcacheConfig .java:

package com.spring.ehcache;

import net.sf.ehcache.CacheManager;

import org.springframework.cache.annotation.EnableCaching;

import org.springframework.cache.ehcache.EhCacheCacheManager;

import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.core.io.ClassPathResource;

@Configuration

@EnableCaching//启用缓存

public classEhcacheConfig {

@Bean(name="ehCacheCacheManager")publicEhCacheCacheManager ehCacheCacheManager(CacheManager cm){

EhCacheCacheManager ehCacheCacheManager= newEhCacheCacheManager();

ehCacheCacheManager.setTransactionAware(true);

ehCacheCacheManager.setCacheManager(cm);returnehCacheCacheManager;

}

@Bean(name="ehCacheManagerFactoryBean")publicEhCacheManagerFactoryBean ehCacheManagerFactoryBean(){

String src= "ehcache.xml";

System.out.println("EhCacheManagerFactoryBean..");

EhCacheManagerFactoryBean ehFactoryBean=

newEhCacheManagerFactoryBean();

ehFactoryBean.setConfigLocation(newClassPathResource(src));returnehFactoryBean;

}

@Bean(name="cm")publicCacheMethod cacheMethod(){return newCacheMethod();

}

}

CacheMethod.java: 这个是缓存测试的类,如果有缓存的话,里面的getStr()方法会执行一次,否则会执行多次

package com.spring.ehcache;

import org.springframework.cache.annotation.Cacheable;public classCacheMethod {publicCacheMethod(){

System.out.println("CacheMethod..");

}

//@Cacheable 表明Spring在调用方法之前,首先应该在缓存中查找方法的返回值。如果这个值能够找到,就返回缓存的值。否则方法被调用,返回值放入缓存中

//@CachePut 表明Spring应该将方式的缓存值放到缓存中。在方法的调用前并不会检查缓存,方法始终都会被调用

//@CacheEvict 表明Spring应该在缓存中清除一个或多个条目

//@Caching 这是一个分组的注解,能够同时应用多个其他的缓存注解

@Cacheable("baseCache")publicString getStr(){

System.out.println("get Str..");return "test get str";

}

}

TestCache.java

package com.spring.ehcache;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.cache.ehcache.EhCacheCacheManager;

import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.springframework.test.context.ContextConfiguration;

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@ContextConfiguration(classes=EhcacheConfig.class)

@RunWith(SpringJUnit4ClassRunner.class)//SpringJUnit4ClassRunner.class使用时要注意,junit的版本要求9以上

public classTestCache {

@AutowiredprivateCacheMethod cm;

@AutowiredprivateEhCacheManagerFactoryBean ehCacheManagerFactoryBean;

@AutowiredprivateEhCacheCacheManager ehCacheCacheManager;/**

* 使用java配置bean

**/@Testpublic voidgetCache(){

System.out.println(ehCacheManagerFactoryBean);

System.out.println(ehCacheCacheManager);

System.out.println(cm.getStr());

System.out.println(cm.getStr());

System.out.println(cm.getStr());

}/**

* 使用xml配置bean

*

public static void main(String[] args) {

ApplicationContext app = new ClassPathXmlApplicationContext("com/spring/ehcache/EhCacheConfig.xml");

System.out.println(app.getBean("ehCacheManagerFactory"));

System.out.println(app.getBean("cacheManager"));

System.out.println(((CacheMethod)app.getBean("cm")).getStr());

System.out.println(((CacheMethod)app.getBean("cm")).getStr());

System.out.println(((CacheMethod)app.getBean("cm")).getStr());

}*/}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值