Question

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"/>
Was it helpful?

Solution

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:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top