2017-12-16 19 views
1

내 애플리케이션은 Spring MVC 애플리케이션입니다. Spring Annotation 기반 캐싱을 사용해 보았습니다. 하지만 작동하지 않습니다.Ehcache가 스프링 캐싱과 작동하지 않습니다. Annotation

   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 [email protected] 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; 
     } 
    .... 
    } 

아래에 내 코드를 참조하지만 findAllUsers() 메서드 호출은, 난 항상 얻을 SYSOUT "GetEmployee라는 방법을 실행 ..."매번 캐시 결코 극복하시기 바랍니다. 무엇이 이것을 일으킬 수 있습니까? 특수 효과 캐싱에 대해 아직 이해하지 못하는 부분이 있습니다.

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; 
    } 
..} 

는 주석 XML 설정에서

답변

1

와 함께 작동하는 메커니즘을 제안하십시오, 당신은

<cache:annotation-driven /> 

은 그래서 봄이 @Cacheable 주석을 선택 추가해야합니다.

자세한 내용은 https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/cache/annotation/EnableCaching.html을 참조하십시오.

@EnableCaching<cache:annotation-driven/>는 필요한 봄의 구성 요소를 등록 할 책임이 있습니다 그 같은 CacheInterceptor 및 호출 스택에 인터셉터를 엮어 프록시 - 또는 AspectJ를 기반 조언으로 전원 주석 중심의 캐시 관리, @Cacheable 메소드가 호출 될 때.

+1

답장을 보내 주셔서 감사합니다. 이제 작동합니다. – giggs