1

実行時に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);
    }

}

私の春のセキュリティ設定で:

<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"/>
4

1 に答える 1

4

UserDetailDaoあなたはコンテキストで宣言されているDispatcherServletので、宣言されているルートWebアプリケーションコンテキストではアクセスできないと思いますuserService。したがって、UserDetailsDaoルートコンテキストでBeanとして宣言する必要があります。

<context:annotation-driven/>また、ルートコンテキストで必要です。

一般的に言えば、Beanの複製があります。BeanはDispatcherServletによってコンテキストに追加され<context:component-scan>、同じクラスのBeanはルートコンテキストで手動で宣言されます。この状況は回避する必要があります。スキャンするパッケージを<context:component-scan>より慎重に指定する必要があります。

参照:

于 2010-11-04T18:00:16.800 に答える