制作Spring 3 MVC控制器方法Transactional

ssk*_*ssk 5 java spring hibernate spring-mvc java-ee

我正在使用Spring 3.1并编写了我的DAO和服务层(事务性).

但是在特殊情况下,为了避免延迟初始化异常,我必须创建一个spring mvc请求处理程序方法@transactional.但它没有将事务附加到该方法.方法名称是ModelAndView home(HttpServletRequest请求,HttpServletResponse响应). http://forum.springsource.org/showthread.php?46814-Transaction-in-MVC-Controller 从这个链接看来,无法将事务(默认情况下)附加到mvc方法.该链接中建议的解决方案似乎适用于Spring 2.5(覆盖handleRequest).任何帮助都会得到真正的认可.谢谢

@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");    
}
Run Code Online (Sandbox Code Playgroud)

Kie*_*ran 4

您需要实现一个接口,以便 Spring 拥有可以用作代理接口的东西:

@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){
.....
}
}
Run Code Online (Sandbox Code Playgroud)