I am developing a project with spring framework. I have following architecture: all database code is in dao classes, all application logic is in service classes, http requests are processed using spring mvc rest controllers.
@Repository
public class UserDao {
@PersistenceContext
private EntityManager em;
public void editUser(User user) {
...
}
}
@Service
public class UserService {
@Autowired
private UserDao userDao;
@Transactional
public void editUser(User user) {
...
}
}
@RestController
@RequestMapping(value = "/UserCtrl", produces = "application/json;charset=UTF-8")
public class UserCtrl {
private static final Logger logger = LogManager.getLogger(AppConfig.LOGGER_NAME);
@Autowired
private ApplicationContext appContext;
@RequestMapping(value = "/editUser")
public ActionResult editUser(@ModelAttribute User user) {
ActionResult rslt = new ActionResult();
try {
UserService userService = appContext.getBean(UserService.class);
userService.editUser(user);
rslt.setSuccess(true);
} catch (Exception ex) {
logger.error("editUser", ex);
rslt.setSuccess(false);
}
return rslt;
}
}
This code works correctly. One thing I cannot understand is how aop proxy of UserService bean starts transaction, when EntityManager is injected only in UserDao class. Can somebody explain me how this works and how EntityManager lifecycle is managed by spring framework ?
Any help appreciated.