5

Spring 3.1を使用していて、DAOとサービスレイヤー(トランザクション)を作成しています。

ただし、怠惰なinit例外を回避するための特別なケースでは、SpringMVCリクエストハンドラーメソッドを@transactionalにする必要があります。しかし、そのメソッドにトランザクションをアタッチできません。メソッド名はModelAndViewhome(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");    
}
4

3 に答える 3

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){
.....
}
}
于 2012-10-19T10:05:27.433 に答える
4

Springは、JDK動的プロキシを使用してトランザクションロジックを実装します。これらは、適切なインターフェイスを実装するプロキシクラスに依存しています。インターフェイスを必要としないCGLibプロキシを使用することも可能です。

このリンクについての素晴らしい記事があります

于 2012-10-19T10:36:57.260 に答える
0

これがあなたのケースに当てはまるかどうかはわかりませんが、個別のDIコンテキストを使用している場合は、Springがどのようにアスペクトを織り込むかも考慮する必要があります。

Springは、またはが使用さ@Transactionalれているのと同じDIコンテキスト内のBeanにのみセマンティクスを適用します。@EnableTransactionManagement<tx:annotation-driven/>

したがって、ルートコンテキストでトランザクション構成を定義すると、そのコンテキストのBeanのみがカバーされます。これは通常、ビジネスサービスのみを意味します。を使用する子コンテキストでは、トランザクション管理を再度有効にする必要があります@Transactional

参照: Spring AOP-親コンテキストで定義されたアスペクトを子コンテキストで機能させる方法は?

于 2021-05-17T15:15:50.940 に答える