@Controller类中的@Transactional方法不被认为是事务性的 [英] @Transactional methods in @Controller class are not considred as transactional

查看:520
本文介绍了@Controller类中的@Transactional方法不被认为是事务性的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到以下内容在标记为@Controller的类中不起作用:

I noticed that the following is not working in a class marked as a @Controller:

@Autowired
SessionFactory sessionFactory;

@ResponseBody
@Transactional
@RequestMapping(method = RequestMethod.GET , value = "/map")

public ArrayList<PhotoDTO> getPhotos(...someParams) {
   Entity result sessionFactory.getCurrentSession()... //do some manipulation

  return result;
}

当我调用URL时,我收到一条错误消息,指出该方法不是事务性的(尽管,如您所见,它被标记为一个)

when I call the URL, I get an error saying that the method is not transactional (although, as you can see, it is marked as one)

如果我将此方法复制到另一个名为MyService的类并从控制器调用它,那么它将很好地工作

If I copy this method to a another class called MyService and call it from the controller instead, it works perfectly

这是某种Spring建议吗(一种使我或多或少使用更多类的阴谋)?

Is this some sort of a Spring advice (a conspiracy to make me use more classes more or less)?

推荐答案

这里可能有两个应用程序上下文:ContextLoaderListener加载的主Spring上下文和DispatcherServlet加载的子级上下文.您还需要将<tx:annotation-driven />放在子上下文加载的配置中.如果您向我们展示您的web.xml文件,也许我可以为您提供更多帮助.

Probably you have two application contexts here: main Spring context loaded by ContextLoaderListener and a child context loaded by DispatcherServlet. You need to put <tx:annotation-driven /> in the configuration loaded by the child context too. If you show us your web.xml file maybe I can help you more.

无论如何,如@NimChimpsky所说,通常不是在控制器层中管理事务的好习惯.

Anyway, as @NimChimpsky says, is usually not a good practice to manage transactions in your controller layer.

这篇关于@Controller类中的@Transactional方法不被认为是事务性的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆