简体   繁体   中英

EhCache with Spring not caching

I have project which i have created using Spring framework. And now thought about caching some data for app. And i take EhCache library for caching. 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

 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. 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:

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

@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

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.

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. When you manually create instance of your object, no proxy is created, and all calls go directly into your object's methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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