简体   繁体   English

带有Spring的EhCache不缓存

[英]EhCache with Spring not caching

I have project which i have created using Spring framework. 我有使用Spring框架创建的项目。 And now thought about caching some data for app. 现在考虑为应用程序缓存一些数据。 And i take EhCache library for caching. 我将EhCache库用于缓存。 This link illistrate how configure it on spring: Spring Caching and Ehcache example It works, but when i am going to change this lines of code 该链接说明了如何在Spring上进行配置: Spring Caching和Ehcache示例它可以工作,但是当我要更改此行代码时

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

to

  MovieDao obj = new MovieDaoImp();

It is stopping work correctly. 它正在正确停止工作。 It calls method everytime. 它每次都调用method。 But i want to use second one. 但是我想用第二个。 What is a difference? 有什么区别? And how i can make so that second one to be worked? 我如何才能使第二个能够工作?

Added some piece of code My spring MVC project structure is below: 添加了一些代码我的Spring MVC项目结构如下:

Controller: 控制器:

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

} }

If any working sample using Spring > 4.1 will be grate 如果有任何使用Spring> 4.1的工作样本将被炉排

When you create instance of object using new ...() , it's no longer a Spring managed bean - hence you lose all functionallity added by Spring, ie aspects (in your case - @Cacheable ), transactions etc. 当您使用new ...()创建对象实例时,它不再是Spring托管的@Cacheable因此,您将失去Spring添加的所有功能,即方面(在您的情况下为@Cacheable ),事务等。

Basically, what Spring does is it creates a proxy for you object, which processes all the calls to the interface methods, adding extra logic depending on the annotations and aspects present. 基本上,Spring所做的是为您的对象创建一个代理,该代理处理对接口方法的所有调用,并根据当前的注释和方面添加额外的逻辑。 When you manually create instance of your object, no proxy is created, and all calls go directly into your object's methods. 手动创建对象实例时,不会创建任何代理,并且所有调用都直接进入对象的方法。

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

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