簡體   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