AEM*_*iji 0 java spring spring-mvc ehcache
我有使用Spring框架创建的项目.现在考虑为app缓存一些数据.我将EhCache库用于缓存.这个链接如何在spring上配置它:Spring Caching和Ehcache示例 它可以工作,但是当我要更改这行代码时
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
MovieDao obj = (MovieDao) context.getBean("movieDao");
Run Code Online (Sandbox Code Playgroud)
至
MovieDao obj = new MovieDaoImp();
Run Code Online (Sandbox Code Playgroud)
它正在停止工作.它每次调用方法.但我想使用第二个.有什么区别?我怎么能让第二个工作?
添加了一些代码 我的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;
}
Run Code Online (Sandbox Code Playgroud)
}
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;
}
Run Code Online (Sandbox Code Playgroud)
}
如果任何使用Spring> 4.1的工作样品将被碾压
当你使用对象创建对象时new ...(),它不再是Spring托管bean - 因此你会失去Spring添加的所有功能,即方面(在你的情况下) - @Cacheable,事务等.
基本上,Spring所做的是为对象创建一个代理,它处理对接口方法的所有调用,根据注释和方面添加额外的逻辑.手动创建对象实例时,不会创建任何代理,并且所有调用都直接进入对象的方法.
| 归档时间: |
|
| 查看次数: |
468 次 |
| 最近记录: |