Spring 休眠安全错误-未找到当前线程的会话
我正在尝试使用用户名和密码对用户进行身份验证,但键入用户详细信息后,错误页面出现错误:Spring 休眠安全错误-未找到当前线程的会话,spring,hibernate,Spring,Hibernate,我正在尝试使用用户名和密码对用户进行身份验证,但键入用户详细信息后,错误页面出现错误: Your login attempt was not successful, try again. Reason: No Session found for current thread Servlet <?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframewo
Your login attempt was not successful, try again.
Reason: No Session found for current thread
Servlet
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org
/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org
/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static
resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in
the /WEB-INF/views directory -->
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.myproject.app" />
<!-- Activates various annotations to be detected in bean classes -->
<context:annotation-config />
<tx:annotation-driven transaction-manager="transactionManager" ></tx:annotation-
driven>
</beans:beans>
服务
@Service("customUserDetailsService")
public class CustomUserDetailsService implements UserDetailsService{
@Autowired
private UserEntityDAO userEntityDAO;
@Autowired
private Assembler assembler;
@Override
@Transactional(readOnly = true)
public UserDetails loadUserByUsername(String username)
throws UsernameNotFoundException {
// TODO Auto-generated method stub
UserDetails userDetails = null;
UserEntity userEntity = userEntityDAO.getUserByName(username);
if (userEntity == null)
throw new UsernameNotFoundException("user not found");
return assembler.buildUserFromUser(userEntity);
}}
我想知道为什么找不到会话 您的DAO实例是三倍的—创建了两个实例(在AutoSCANK和另一个显式bean声明的不同上下文中)考虑将您的服务和TX管理器移到根Web应用程序上下文,并使用该类是“代码> UserEntityDAO < /COD>不<代码> UserDaoImpl <代码>,这在问题中显示。谢谢,这就是问题所在。
@Repository
@Transactional
public class UserDAOImpl implements UserDAO{
@Autowired
private SessionFactory sessionFactory;
@Override
@Transactional
public User getUserByName(String username) {
// TODO Auto-generated method stub
User user = (User) sessionFactory.getCurrentSession().createQuery(
"select u from UserEntity u where u.username = '' +
username + ''").uniqueResult();
return user;
}
@Service("customUserDetailsService")
public class CustomUserDetailsService implements UserDetailsService{
@Autowired
private UserEntityDAO userEntityDAO;
@Autowired
private Assembler assembler;
@Override
@Transactional(readOnly = true)
public UserDetails loadUserByUsername(String username)
throws UsernameNotFoundException {
// TODO Auto-generated method stub
UserDetails userDetails = null;
UserEntity userEntity = userEntityDAO.getUserByName(username);
if (userEntity == null)
throw new UsernameNotFoundException("user not found");
return assembler.buildUserFromUser(userEntity);
}}