@Cachebale不使用Ehcache和spring MVC Ehcache不使用spring缓存注释

@Cachebale不使用Ehcache和spring MVC Ehcache不使用spring缓存注释,spring,caching,annotations,ehcache,Spring,Caching,Annotations,Ehcache,我的应用程序是SpringMVC应用程序。尝试使用基于Spring注释的缓存。但它不起作用。请参考下面我的代码 1. pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="htt

我的应用程序是SpringMVC应用程序。尝试使用基于Spring注释的缓存。但它不起作用。请参考下面我的代码

                1. pom.xml
                <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
                    <modelVersion>4.0.0</modelVersion>
                    <groupId>com.giggal.spring</groupId>
                    <artifactId>spring-tutorial-mvc</artifactId>
                    <version>0.0.1-SNAPSHOT</version>
                    <packaging>war</packaging>
                ......
                <dependencies>
                        <dependency>
                            <groupId>org.springframework</groupId>
                            <artifactId>spring-context</artifactId>
                            <version>4.3.4.RELEASE</version>
                        </dependency>
                        <dependency>
                            <groupId>org.springframework</groupId>
                            <artifactId>spring-context-support</artifactId>
                            <version>4.3.1.RELEASE</version>
                        </dependency>
                        <dependency>
                            <groupId>org.springframework</groupId>
                            <artifactId>spring-core</artifactId>
                            <version>4.3.4.RELEASE</version>
                        </dependency>
                        <dependency>
                            <groupId>org.springframework</groupId>
                            <artifactId>spring-beans</artifactId>
                            <version>4.3.4.RELEASE</version>
                        </dependency>
                        <dependency>
                            <groupId>org.springframework</groupId>
                            <artifactId>spring-web</artifactId>
                            <version>4.3.4.RELEASE</version>
                        </dependency>
                        <dependency>
                            <groupId>org.springframework</groupId>
                            <artifactId>spring-webmvc</artifactId>
                            <version>4.3.4.RELEASE</version>
                        </dependency>
                        <dependency>
                            <groupId>org.springframework</groupId>
                            <artifactId>spring-jdbc</artifactId>
                            <version>4.3.4.RELEASE</version>
                        </dependency>
                        <!-- Servlet -->
                        <dependency>
                            <groupId>javax.servlet</groupId>
                            <artifactId>servlet-api</artifactId>
                            <version>2.5</version>
                            <scope>provided</scope>
                        </dependency>
                        <dependency>
                            <groupId>javax.servlet.jsp</groupId>
                            <artifactId>jsp-api</artifactId>
                            <version>2.1</version>
                            <scope>provided</scope>
                        </dependency>
                        <dependency>
                            <groupId>javax.servlet</groupId>
                            <artifactId>jstl</artifactId>
                            <version>1.2</version>
                        </dependency>
                        <!-- EHCache -->
                        <dependency>
                            <groupId>net.sf.ehcache</groupId>
                            <artifactId>ehcache</artifactId>
                            <version>2.10.2.2.21</version>
                        </dependency>

                    </dependencies>
                </project>

2. My Spring app consists of two context xml config files, the first one app-ctx.xml only scans non-@Controller annotated beans:

            <beans xmlns="http://www.springframework.org/schema/beans"
                xmlns:context="http://www.springframework.org/schema/context"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
                xmlns:aop="http://www.springframework.org/schema/aop" 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/context 
               http://www.springframework.org/schema/context/spring-context-3.0.xsd
               http://www.springframework.org/schema/aop 
               http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
               http://www.springframework.org/schema/cache
               http://www.springframework.org/schema/cache/spring-cache.xsd">

                <context:component-scan base-package="com.giggal.spring">
                    <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation" />
                </context:component-scan>
                <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
                    <property name="cacheManager" ref="ehcache" />
                </bean>
                <bean id="ehcache"
                    class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
                    <property name="configLocation" value="classpath:ehcache.xml" />
                    <property name="shared" value="true" />
                </bean>

            .....

            </beans>

3. The second one mvc-config.xml contains all spring-mvc setup and scans @Controller annotated beans

                <?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:context="http://www.springframework.org/schema/context"
                    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
                        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">

                    <context:component-scan base-package="com.giggal.spring" use-default-filters="false">
                        <context:include-filter expression="org.springframework.stereotype.Controller"
                            type="annotation" />
                    </context:component-scan>


                    <mvc:annotation-driven />
                    <!-- 2. HandlerMapping : Used default handler mapping internally -->

                    <!-- 3. ViewResolver -->
                    <bean
                        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                        <!-- Example: a logical view name of 'showMessage' is mapped to '/WEB-INF/jsp/showMessage.jsp' -->
                        <property name="prefix" value="/WEB-INF/view/" />
                        <property name="suffix" value=".jsp" />
                    </bean>

                </beans>

4. The ehcache.xml used for caching with cache name emp :
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
    <defaultCache eternal="true" maxElementsInMemory="100"
        overflowToDisk="false" />
    <cache name="emp" maxElementsInMemory="10000" eternal="true"
        overflowToDisk="false" />
</ehcache>


5. The controller class UserController

@Controller
@RequestMapping(value = "/user/")
public class UserController {
@Autowired
    UserService userService; // Service which will do all data
                                // retrieval/manipulation work

@RequestMapping(value = "/getAllUsers/", method = RequestMethod.GET)
    public ModelAndView listAllUsers() {
        ModelAndView mav = new ModelAndView("user/user_list");
        List<User> users = userService.findAllUsers();
        mav.addObject("users", users);
        return mav;
    }

......
}

6. UserService class:

    @Service("userService")
    @Transactional
    public class UserServiceImpl implements UserService {

        private static final AtomicLong counter = new AtomicLong();

        private static List<User> users;

        static {
            users = populateDummyUsers();
        }

        @Cacheable("emp") 
        public List<User> findAllUsers() {
            System.out.println("execute getEmployee method..");
            return users;
        }  
    ....
    }
1。pom.xml
4.0.0
com.giggal.spring
spring教程mvc
0.0.1-快照
战争
......
org.springframework
spring上下文
4.3.4.1发布
org.springframework
spring上下文支持
4.3.1.1发布
org.springframework
弹簧芯
4.3.4.1发布
org.springframework
春豆
4.3.4.1发布
org.springframework
弹簧网
4.3.4.1发布
org.springframework
SpringWebMVC
4.3.4.1发布
org.springframework
SpringJDBC
4.3.4.1发布
javax.servlet
servlet api
2.5
假如
javax.servlet.jsp
jsp api
2.1
假如
javax.servlet
jstl
1.2
net.sf.ehcache
ehcache
2.10.2.2.21
2.我的Spring应用程序由两个上下文xml配置文件组成,第一个app-ctx.xml仅扫描非@Controller注释的bean:
.....
3.第二个mvc-config.xml包含所有SpringMVC设置和扫描@Controller注释bean
4.用于缓存名为emp的缓存的ehcache.xml:
5.控制器类UserController
@控制器
@请求映射(value=“/user/”)
公共类用户控制器{
@自动连线
UserService UserService;//将处理所有数据的服务
//检索/操作工作
@RequestMapping(value=“/getAllUsers/”,method=RequestMethod.GET)
公共模型和视图列表用户(){
ModelAndView mav=新的ModelAndView(“用户/用户列表”);
List users=userService.findAllUsers();
mav.addObject(“用户”,用户);
返回mav;
}
......
}
6.用户服务类:
@服务(“用户服务”)
@交易的
公共类UserServiceImpl实现UserService{
私有静态最终AtomicLong计数器=新AtomicLong();
私有静态列表用户;
静止的{
users=populateDummyUsers();
}
@可缓存(“emp”)
公共列表findAllUsers(){
System.out.println(“执行getEmployee方法…”);
返回用户;
}  
....
}
但是findAllUsers()方法调用永远不会被缓存,每次我都会得到sysout“execute getEmployee method..”。这可能是什么原因造成的?关于缓存注释,我肯定有一些东西还不了解

But the same code is working as below
1. 
@Configuration
@EnableCaching
@ComponentScan({ "com.giggal.*" })
public class AppConfig {

    @Bean
    public CacheManager cacheManager() {
        return new EhCacheCacheManager(ehCacheCacheManager().getObject());
    }

    @Bean
    public EhCacheManagerFactoryBean ehCacheCacheManager() {
        EhCacheManagerFactoryBean cmfb = new EhCacheManagerFactoryBean();
        cmfb.setConfigLocation(new ClassPathResource("ehcache.xml"));
        cmfb.setShared(true);
        return cmfb;
    }
}

---
@Controller
@RequestMapping(value = "/user/")
public class UserController {



@RequestMapping(value = "/getAllUsers/", method = RequestMethod.GET)
    public ModelAndView listAllUsers() {
           AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(com.giggal.spring.AppConfig.class);
            UserService obj = (UserService) context.getBean("userService");
            List<User> users = obj.findAllUsers();
        ModelAndView mav = new ModelAndView("user/user_list");
        //List<User> users = userService.findAllUsers();
        mav.addObject("users", users);
        return mav;
    }
..}
但相同的代码如下所示
1.
@配置
@启用缓存
@组件扫描({“com.giggal.*})
公共类AppConfig{
@豆子
公共缓存管理器缓存管理器(){
返回新的EhcacheManager(EhcacheManager().getObject());
}
@豆子
公共EhCacheManagerFactoryBean EHCacheManager(){
EhCacheManagerFactoryBean cmfb=新的EhCacheManagerFactoryBean();
setConfigLocation(新类路径资源(“ehcache.xml”);
cmfb.setShared(真);
返回cmfb;
}
}
---
@控制器
@请求映射(value=“/user/”)
公共类用户控制器{
@RequestMapping(value=“/getAllUsers/”,method=RequestMethod.GET)
公共模型和视图列表用户(){
AnnotationConfigApplicationContext上下文=新的AnnotationConfigApplicationContext(com.giggal.spring.AppConfig.class);
UserService obj=(UserService)context.getBean(“UserService”);
List users=obj.findAllUsers();
ModelAndView mav=新的ModelAndView(“用户/用户列表”);
//List users=userService.findAllUsers();
mav.addObject(“用户”,用户);
返回mav;
}
..}

请建议在xml配置中使用注释的机制,您需要添加

<cache:annotation-driven />

所以春天来了<