自动装配注释失败

发布于 2024-10-01 04:03:44 字数 1242 浏览 0 评论 0原文

为什么我在执行时总是得到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风挽心 2024-10-08 04:03:44

我猜您的 UserDetailDao 是在 DispatcherServlet 上下文中声明的,因此在声明 userService 的根 webapp 上下文中无法访问。因此,UserDetailsDao 应该在根上下文中声明为 bean。

此外,您还需要根上下文中的

一般来说,您现在有重复的bean - 通过将bean添加到DispatcherServlet上下文中,并且手动将相同类的bean添加到DispatcherServlet上下文中在根上下文中声明。应避免这种情况 - 您需要更仔细地指定要由 扫描的包。

另请参阅:

I guess your UserDetailDao is declared in the DispatcherServlet context and therefore is not accessible in the root webapp context where userService 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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文