【发布时间】:2014-10-21 16:31:16
【问题描述】:
我在 Spring 中有休息风格的控制器。在控制器中,我注入了 dao 接口。从控制器我持久化数据。换句话说,我喜欢 REST Web 服务。人们向我发送数据,我坚持下去。
/**
* Payment rest controller which receives
* JSON of data
*/
@Controller
@RequestMapping("/data")
public class PaymentTransaction {
@Autowired
private TestDao dao;
@RequestMapping(value = "/test", method = RequestMethod.POST)
@ResponseBody()
public String test(HttpServletRequest request) {
...
}
目前我在 Dao 类中有 @transaction 注解。例如:
import org.springframework.transaction.annotation.Transactional;
@Component
@Transactional
public interface TestDao {
@Transactional(propagation = Propagation.REQUIRED)
public void first();
}
I have read that this is very bad style. Using this answer at stackoverflow ,这里是解释和示例为什么这是不好的 - 我们不能在 DAO 和控制器中添加这个注释。我们必须将它添加到服务层。
但是我不明白什么是服务层?或者它在哪里?我没有这样的东西。 我应该在哪里写@Transactional注解?
最好的问候,
【问题讨论】:
标签: spring spring-mvc jpa