jar包:
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:tx=”http://www.springframework.org/schema/tx”
xmlns:aop=”http://www.springframework.org/schema/aop”
xmlns:context=”http://www.springframework.org/schema/context”
xmlns:util=”http://www.springframework.org/schema/util”
xmlns:p=”http://www.springframework.org/schema/p”
xmlns:cache=”http://www.springframework.org/schema/cache”
xsi:schemaLocation=”http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd”>
<!– 隐式地向 Spring 容器注册
AutowiredAnnotationBeanPostProcessor、
CommonAnnotationBeanPostProcessor、
PersistenceAnnotationBeanPostProcessor 以及
equiredAnnotationBeanPostProcessor 这 4 个 BeanPostProcessor。
在配置文件中使用 context 命名空间之前,必须在 <beans> 元素中声明 context 命名空间。 –>
<context:annotation-config/>
<!– <context:component-scan/> 配置项不但启用了对类包进行扫描
以实施注释驱动 Bean 定义的功能,
同时还启用了注释驱动自动注入的功能
(即还隐式地在内部注册了 AutowiredAnnotationBeanPostProcessor
和 CommonAnnotationBeanPostProcessor) –>
<context:component-scan base-package=”com.*” />
<!– 缓存注解驱动 –>
<cache:annotation-driven />
<!– cacheManager工厂类 –>
<bean id=”cacheManagerFactory” class=”org.springframework.cache.ehcache.EhCacheManagerFactoryBean”
p:configLocation=”classpath:ehcache.xml”
p:shared=”false” />
<!– 声明cacheManager –>
<bean id=”cacheManager” class=”org.springframework.cache.ehcache.EhCacheCacheManager”
p:cacheManager-ref=”cacheManagerFactory”/>
</beans>
spring-servlet.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:mvc=”http://www.springframework.org/schema/mvc”
xmlns:p=”http://www.springframework.org/schema/p”
xmlns:context=”http://www.springframework.org/schema/context”
xsi:schemaLocation=”
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd”
default-autowire=”byName”>
<!–访问到静态的文件
<mvc:default-servlet-handler/>
启动SpringMVC的注解功能,它会自动注册HandlerMapping、HandlerAdapter、ExceptionResolver的相关实例
<mvc:annotation-driven/>–>
<context:component-scan base-package=”com.*”/>
<!–通用视图解析器 –>
<bean id=”viewResolver”
class=”org.springframework.web.servlet.view.InternalResourceViewResolver”>
<property name=”viewClass”
value=”org.springframework.web.servlet.view.JstlView” />
<property name=”prefix” value=”/WEB-INF/jsp/” />
<property name=”suffix” value=”.jsp” />
</bean>
</beans>
ehcache.xml
<?xml version=”1.0″ encoding=”UTF-8″?>
<ehcache>
<diskStore path=”D:/tmpdir”/>
<defaultCache
maxElementsInMemory=”500″
eternal=”false”
timeToIdleSeconds=”300″
timeToLiveSeconds=”1200″
overflowToDisk=”true”/>
<cache name=”young”
maxElementsInMemory=”150″
eternal=”false”
timeToLiveSeconds=”36000″
timeToIdleSeconds=”3600″
overflowToDisk=”true”/>
</ehcache>
Controller
package com.controller;
import javax.annotation.Resource;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.service.ITestService;
@Controller
public class TestController {
@Resource(name=”testService”)
private ITestService testService;
@Resource
CacheManager manager;
@RequestMapping(“/hello.do”)
public void controller(){
String s = testService.Test(“Key”);
System.out.println(s);
String[] names = manager.getCacheNames();
for (int i = 0; i < names.length; i++) {// 输出全部缓存
System.out.println(names[i]);
}
Cache young = manager.getCache(“young”);// 得到cache
System.out.println(“Cache named “young”:” + young);
// System.out.println(young.get(“s”).getValue());
// System.out.println(young.get(“surname”).getValue());
// System.out.println(young.get(“Key”));
// System.out.println(young.get(“Key”).getObjectKey());
// System.out.println(young.get(“Key”).getObjectValue());
System.out.println(“end!”);
}
}
Service
package com.service;
public interface ITestService {
public String Test(String surname);
}
serviceImpl
/**
* 项目名:国土资源厅统计分析与应用系统
* 包名:com.service.impl
* 文件名:TestServiceImpl.java
* 版本信息:V1.0.0
* 日期:2014-4-16-上午10:44:05
* Copyright (c) 2014卓越天成软件公司-版权全部
*
*/
package com.service.impl;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.service.ITestService;
@Service(“testService”)
public class TestServiceImpl implements ITestService {
@Override
@Cacheable(value = “young”, key = “#surname”)
public String Test(String surname) {
System.out.println(“方法开始~!”);
return “tsdfsd”;
}
}
web.xml及log4j.properties及jsp页面就不贴出来了,现在无法缓存,求指导,在junit4可以缓存的
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:tx=”http://www.springframework.org/schema/tx”
xmlns:aop=”http://www.springframework.org/schema/aop”
xmlns:context=”http://www.springframework.org/schema/context”
xmlns:util=”http://www.springframework.org/schema/util”
xmlns:p=”http://www.springframework.org/schema/p”
xmlns:cache=”http://www.springframework.org/schema/cache”
xsi:schemaLocation=”http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd”>
<!– 隐式地向 Spring 容器注册
AutowiredAnnotationBeanPostProcessor、
CommonAnnotationBeanPostProcessor、
PersistenceAnnotationBeanPostProcessor 以及
equiredAnnotationBeanPostProcessor 这 4 个 BeanPostProcessor。
在配置文件中使用 context 命名空间之前,必须在 <beans> 元素中声明 context 命名空间。 –>
<context:annotation-config/>
<!– <context:component-scan/> 配置项不但启用了对类包进行扫描
以实施注释驱动 Bean 定义的功能,
同时还启用了注释驱动自动注入的功能
(即还隐式地在内部注册了 AutowiredAnnotationBeanPostProcessor
和 CommonAnnotationBeanPostProcessor) –>
<context:component-scan base-package=”com.*” />
<!– 缓存注解驱动 –>
<cache:annotation-driven />
<!– cacheManager工厂类 –>
<bean id=”cacheManagerFactory” class=”org.springframework.cache.ehcache.EhCacheManagerFactoryBean”
p:configLocation=”classpath:ehcache.xml”
p:shared=”false” />
<!– 声明cacheManager –>
<bean id=”cacheManager” class=”org.springframework.cache.ehcache.EhCacheCacheManager”
p:cacheManager-ref=”cacheManagerFactory”/>
</beans>
spring-servlet.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:mvc=”http://www.springframework.org/schema/mvc”
xmlns:p=”http://www.springframework.org/schema/p”
xmlns:context=”http://www.springframework.org/schema/context”
xsi:schemaLocation=”
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd”
default-autowire=”byName”>
<!–访问到静态的文件
<mvc:default-servlet-handler/>
启动SpringMVC的注解功能,它会自动注册HandlerMapping、HandlerAdapter、ExceptionResolver的相关实例
<mvc:annotation-driven/>–>
<context:component-scan base-package=”com.*”/>
<!–通用视图解析器 –>
<bean id=”viewResolver”
class=”org.springframework.web.servlet.view.InternalResourceViewResolver”>
<property name=”viewClass”
value=”org.springframework.web.servlet.view.JstlView” />
<property name=”prefix” value=”/WEB-INF/jsp/” />
<property name=”suffix” value=”.jsp” />
</bean>
</beans>
ehcache.xml
<?xml version=”1.0″ encoding=”UTF-8″?>
<ehcache>
<diskStore path=”D:/tmpdir”/>
<defaultCache
maxElementsInMemory=”500″
eternal=”false”
timeToIdleSeconds=”300″
timeToLiveSeconds=”1200″
overflowToDisk=”true”/>
<cache name=”young”
maxElementsInMemory=”150″
eternal=”false”
timeToLiveSeconds=”36000″
timeToIdleSeconds=”3600″
overflowToDisk=”true”/>
</ehcache>
Controller
package com.controller;
import javax.annotation.Resource;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.service.ITestService;
@Controller
public class TestController {
@Resource(name=”testService”)
private ITestService testService;
@Resource
CacheManager manager;
@RequestMapping(“/hello.do”)
public void controller(){
String s = testService.Test(“Key”);
System.out.println(s);
String[] names = manager.getCacheNames();
for (int i = 0; i < names.length; i++) {// 输出全部缓存
System.out.println(names[i]);
}
Cache young = manager.getCache(“young”);// 得到cache
System.out.println(“Cache named “young”:” + young);
// System.out.println(young.get(“s”).getValue());
// System.out.println(young.get(“surname”).getValue());
// System.out.println(young.get(“Key”));
// System.out.println(young.get(“Key”).getObjectKey());
// System.out.println(young.get(“Key”).getObjectValue());
System.out.println(“end!”);
}
}
Service
package com.service;
public interface ITestService {
public String Test(String surname);
}
serviceImpl
/**
* 项目名:国土资源厅统计分析与应用系统
* 包名:com.service.impl
* 文件名:TestServiceImpl.java
* 版本信息:V1.0.0
* 日期:2014-4-16-上午10:44:05
* Copyright (c) 2014卓越天成软件公司-版权全部
*
*/
package com.service.impl;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.service.ITestService;
@Service(“testService”)
public class TestServiceImpl implements ITestService {
@Override
@Cacheable(value = “young”, key = “#surname”)
public String Test(String surname) {
System.out.println(“方法开始~!”);
return “tsdfsd”;
}
}
web.xml及log4j.properties及jsp页面就不贴出来了,现在无法缓存,求指导,在junit4可以缓存的
解决方案