繁体   English   中英

带有Spring的EhCache不缓存

[英]EhCache with Spring not caching

我有使用Spring框架创建的项目。 现在考虑为应用程序缓存一些数据。 我将EhCache库用于缓存。 该链接说明了如何在Spring上进行配置: Spring Caching和Ehcache示例它可以工作,但是当我要更改此行代码时

 ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
 MovieDao obj = (MovieDao) context.getBean("movieDao");

  MovieDao obj = new MovieDaoImp();

它正在正确停止工作。 它每次都调用method。 但是我想用第二个。 有什么区别? 我如何才能使第二个能够工作?

添加了一些代码我的Spring MVC项目结构如下:

控制器:

@Controller 
public class MainController {

@Autowired
private BackEndService backEnd;

@RequestMapping(value = "/home", method = RequestMethod.GET)
  public ModelAndView viewDefault(Model model) throws Exception {
     model.addAttribute("categories", getCategoriesForGuest())
     return new ModelAndView(JspView.Home, model.asMap());
  }

 //@Cacheable(value = "categoriesCache")
 //first i want to make cachable this method. Not worked
 private List<Category> getCategoriesForGuest() {
    //i am going to cache method(guestListPaymentCategories()), but not worked neither.
    List<Category> categoriesResult = backEnd
   .guestListPaymentCategories()
   .getCategories()
   .getCategory();

   return categoriesResult;
}

}

BackEndService.java

@Service
public class BackEndService {
  protected WcfBackendService_Service service;
  protected WcfBackendService port;

  public BackEndService () {
        service = new WcfBackendService_Service();
        port = service.getSOAP();
  }

@Cacheable(value = "categoriesCache")
public ListCategoriesResult guestListPaymentCategories() {
    ListCategoriesResult result = null;
    try {
        result = port.guestListPaymentCategories(request);
        if (result.getResultCodes() == ResultCodes.OK) {
            return result;
        } else {
            throw new Exception(result.getDescription());
        }
    } catch (Exception ex) {
        log.error(ex.getMessage(), ex);
    }

    return result;
}

}

如果有任何使用Spring> 4.1的工作样本将被炉排

当您使用new ...()创建对象实例时,它不再是Spring托管的@Cacheable因此,您将失去Spring添加的所有功能,即方面(在您的情况下为@Cacheable ),事务等。

基本上,Spring所做的是为您的对象创建一个代理,该代理处理对接口方法的所有调用,并根据当前的注释和方面添加额外的逻辑。 手动创建对象实例时,不会创建任何代理,并且所有调用都直接进入对象的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM