自动装配注释失败
为什么我在执行时总是得到 userDetailDao 异常 null :
package com.springweb.service;
import com.springweb.dao.UserDetailDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.dao.DataAccessException;
import org.springframework.security.userdetails.UserDetails;
import org.springframework.security.userdetails.UserDetailsService;
import org.springframework.security.userdetails.UsernameNotFoundException;
public class UserService implements UserDetailsService
{
@Autowired
UserDetailDao userDetailDao;
public UserDetails loadUserByUsername(String string) throws UsernameNotFoundException, DataAccessException {
return userDetailDao.queryForUser(string);
}
}
在我的 spring 安全配置中:
<security:authentication-provider user-service-ref="userService">
<security:password-encoder hash="md5" />
</security:authentication-provider>
<bean name="userService" class="com.springweb.service.UserService">
</bean>
在我的调度程序上下文中,我已经定义了扫描包:
<context:component-scan base-package="com.springweb"/>
why i always get userDetailDao exception null when executed :
package com.springweb.service;
import com.springweb.dao.UserDetailDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.dao.DataAccessException;
import org.springframework.security.userdetails.UserDetails;
import org.springframework.security.userdetails.UserDetailsService;
import org.springframework.security.userdetails.UsernameNotFoundException;
public class UserService implements UserDetailsService
{
@Autowired
UserDetailDao userDetailDao;
public UserDetails loadUserByUsername(String string) throws UsernameNotFoundException, DataAccessException {
return userDetailDao.queryForUser(string);
}
}
at my spring security config :
<security:authentication-provider user-service-ref="userService">
<security:password-encoder hash="md5" />
</security:authentication-provider>
<bean name="userService" class="com.springweb.service.UserService">
</bean>
and at my dispatcher context, i already define to scan package:
<context:component-scan base-package="com.springweb"/>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜您的
UserDetailDao
是在DispatcherServlet
上下文中声明的,因此在声明userService
的根 webapp 上下文中无法访问。因此,UserDetailsDao
应该在根上下文中声明为 bean。此外,您还需要根上下文中的
。一般来说,您现在有重复的bean - 通过
将bean添加到DispatcherServlet
上下文中,并且手动将相同类的bean添加到DispatcherServlet
上下文中在根上下文中声明。应避免这种情况 - 您需要更仔细地指定要由
扫描的包。另请参阅:
I guess your
UserDetailDao
is declared in theDispatcherServlet
context and therefore is not accessible in the root webapp context whereuserService
is declared. So,UserDetailsDao
should be declared as a bean in the root context.Also you need
<context:annotation-driven/>
in the root context.Generally speaking, you have a duplication of beans now - beans are added to the
DispatcherServlet
context by<context:component-scan>
, and beans of the same classes are manually declared in the root context. This situation should be avoided - you need to specify packages to be scanned by<context:component-scan>
more carefully.See also: