복붙노트

[SPRING] Spring 3 MVC 컨트롤러 메소드 만들기 Transactional

SPRING

Spring 3 MVC 컨트롤러 메소드 만들기 Transactional

저는 Spring 3.1을 사용하고 DAO와 서비스 계층 (트랜잭션)을 작성했습니다.

그러나 특별한 경우에 게으른 초기화 예외를 피하기 위해 스프링 MVC 요청 처리기 메소드 @transactional을 만들어야합니다. 그러나 그 방법에 거래를 첨부하지 못하고있다. 메소드 이름은 ModelAndView 홈 (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");    
}

해결법

  1. ==============================

    1.Spring이 Proxy 인터페이스로 사용할 수있는 인터페이스를 구현해야합니다.

    Spring이 Proxy 인터페이스로 사용할 수있는 인터페이스를 구현해야합니다.

    @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){
    .....
    }
    }
    
  2. ==============================

    2.Spring은 JDK 동적 프록시를 사용하여 트랜잭션 로직을 구현하며, 적절한 인터페이스를 구현하는 프록시 클래스에 의존한다. 인터페이스가 필요없는 CGLib 프록시를 사용할 수도 있습니다.

    Spring은 JDK 동적 프록시를 사용하여 트랜잭션 로직을 구현하며, 적절한 인터페이스를 구현하는 프록시 클래스에 의존한다. 인터페이스가 필요없는 CGLib 프록시를 사용할 수도 있습니다.

    이 링크에 대한 멋진 기사가 있습니다.

  3. from https://stackoverflow.com/questions/12971848/making-spring-3-mvc-controller-method-transactional by cc-by-sa and MIT license