使 Spring 3 MVC 控制器方法事务性 [英] Making Spring 3 MVC controller method Transactional
问题描述
我正在使用 Spring 3.1 并编写了我的 DAO 和服务层(事务层).
I am using Spring 3.1 and have my DAO and service layer(transactional) written.
但是在特殊情况下,为了避免延迟初始化异常,我必须创建一个 spring mvc 请求处理程序方法 @transactional.但是它无法将事务附加到该方法.方法名称是 ModelAndView home(HttpServletRequest request, HttpServletResponse response).http://forum.springsource.org/showthread.php?46814-Transaction-in-MVC-Controller从这个链接看来,不可能将事务(默认情况下)附加到 mvc 方法.该链接中建议的解决方案似乎适用于 Spring 2.5(覆盖 handleRequest ).任何帮助将不胜感激.谢谢
However in a special case to avoid a lazy init exception I have to make a spring mvc request handler method @transactional. But It is failing to attach transaction to that method. Method name is ModelAndView home(HttpServletRequest request, HttpServletResponse response). http://forum.springsource.org/showthread.php?46814-Transaction-in-MVC-Controller From this link it seems it is not possible to attach transaction (by default ) to mvc methods. Solution suggested in that link seems to be for Spring 2.5 (overriding handleRequest ). Any help will be reallyappreciated. Thanks
@Controller
public class AuthenticationController {
@Autowired
CategoryService categoryService;
@Autowired
BrandService brandService;
@Autowired
ItemService itemService;
@RequestMapping(value="/login.html",method=RequestMethod.GET)
ModelAndView login(){
return new ModelAndView("login.jsp");
}
@RequestMapping(value="/home.html",method=RequestMethod.GET)
@Transactional
ModelAndView home(HttpServletRequest request, HttpServletResponse response){
List<Category> categories = categoryService.readAll();
request.setAttribute("categories", categories);
List<Brand> brands = brandService.readAll();
request.setAttribute("brands", brands);
List<Item> items = itemService.readAll();
request.setAttribute("items", items);
Set<Image> images = items.get(0).getImages();
for(Image i : images ) {
System.out.println(i.getUrl());
}
return new ModelAndView("home.jsp");
}
推荐答案
您需要实现一个接口,以便 Spring 可以将其用作代理接口:
You'll need to implement an interface so that Spring has something it can use as a Proxy interface:
@Controller
public interface AuthenticationController {
ModelAndView home(HttpServletRequest request, HttpServletResponse response);
}
@Controller
public class AuthenticationControllerImpl implements AuthenticationController {
@RequestMapping(value="/home.html",method=RequestMethod.GET)
@Transactional
@Override
ModelAndView home(HttpServletRequest request, HttpServletResponse response){
.....
}
}
这篇关于使 Spring 3 MVC 控制器方法事务性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!