HibernateException当从控制器调用服务但在我的测试中没有找到当前线程的会话
当我启动应用程序并从我的控制器调用服务时,我收到No session foundexception。 但是,我可以通过unit testing成功调用相同的服务。
当我在控制器类上调用userService.userExists(username)方法时会发生这种情况。 我开始在调试器中跟踪执行,发现即使我将服务装饰为事务性,TransactionSynchronizationManager资源对象映射也是空的。 但是,当我从unit testing中调用服务时,TransactionSynchronizationManager上有资源。
控制器类:
@Controller public class DiaryController { @Autowired private UserService userService; @RequestMapping(method = RequestMethod.GET, value = "/diary/{username}") public String home(@PathVariable String username, ModelMap model) { String authUser = SecurityContextHolder.getContext().getAuthentication().getName(); if(authUser.equals(username)) { model.addAttribute("message", "Hey there. How's it going " + username + "?"); } else if (userService.userExists(username)) { model.addAttribute("message", "You're looking at diary for " + username + "."); } else { model.addAttribute("message", "User " + username + " does not exist."); } return "diary"; } }
Hibernate上下文:
classpath:database.properties classpath*:hbm/User.hbm.xml org.hibernate.dialect.MySQLDialect true
应用背景:
UserDoaImpl:
@Repository public class UserDaoImpl extends AbstractDaoImpl implements UserDao { protected UserDaoImpl() { super(User.class); } @Override public void saveUser(User user) { saveOrUpdate(user); } @Override public List findUsers(String username) { return findByCriteria(Restrictions.like("username", username, MatchMode.EXACT)); } @Override public boolean exists(String username) { return findById(username) != null; } }
我的工作单位测试:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:/spring/applicationContext.xml", "classpath:/spring/securityContext.xml", "classpath:/spring/hibernateContext.xml" }) public class UserServiceTest { @Autowired private UserService userService; @Test public void userServiceTest() { Assert.assertTrue(userService.userExists("fergal")); } }
UserServiceImpl:
@Service("userService") @Transactional(readOnly = true) public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; @Override public User findByUserName(String userName) { return userDao.findById(userName); } @Override @Transactional(readOnly = false) public void saveUser(User user) { userDao.saveUser(user); } @Override @Transactional(readOnly = false) public void deleteUser(String userName) { User user = userDao.findById(userName); if (user != null) { userDao.delete(user); } } @Override public List findUsers(String user) { return userDao.findUsers(user); } @Override public boolean userExists(String username) { return userDao.exists(username); }
web.xml中
MyApp myapp-dispatcher org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:/spring/applicationContext.xml 1 myapp-dispatcher / contextConfigLocation classpath:/spring/** springSecurityFilterChain org.springframework.web.filter.DelegatingFilterProxy springSecurityFilterChain /* org.springframework.web.context.ContextLoaderListener
将以下filter添加到web.xml。 您可以在http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/orm/hibernate4/support/OpenSessionInViewFilter.html查看有关此filter的更多信息。
hibernateFilter org.springframework.orm.hibernate4.support.OpenSessionInViewFilter sessionFactoryBeanName sessionFactory hibernateFilter /*