Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Autowire不会连接自定义用户详细信息服务_Java_Spring_Spring Mvc_Spring Security - Fatal编程技术网

Java Autowire不会连接自定义用户详细信息服务

Java Autowire不会连接自定义用户详细信息服务,java,spring,spring-mvc,spring-security,Java,Spring,Spring Mvc,Spring Security,我已经定义了定制的UserDetailService,命名为UserService,它可以正常工作。当我在某个bean中自动连接它时,它会给我错误 org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.restaurant.service.UserService] found for dependency: expected at least 1 bean wh

我已经定义了定制的
UserDetailService
,命名为
UserService
,它可以正常工作。当我在某个bean中自动连接它时,它会给我错误

org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.restaurant.service.UserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
我已经在这个论坛上检查了所有与此相关的答案,但它没有帮助我解决我的问题,所以我再次发布了这个问题

在我的
rootcontext.xml
中,我包含了“
context:annotation config
”标记,在我的
webcontext.xml
中,我包含了
“context:component scan base package=“com.restaurant”“
。我还注意到,
UserService
的默认构造函数在服务器启动时被调用。但仍然自动将其连接到其他bean中不起作用。除custom
UserDetailService
之外的所有其他autowire工作

谁能帮我解决这个问题,因为我完全陷入了这个问题

我的自定义UserService类如下所示:

@Service
public class UserService implements UserDetailsService
{

@Autowired
private UserDAO userDAO;    


public UserDetails loadUserByUsername(String username)throws UsernameNotFoundException, DataAccessException {       
    User user = userDAO.findUnique("select usr from User usr where usr.isActive = 1 and usr.userName = ?", username);       
    if(user == null) {
        throw new UsernameNotFoundException("User not found");
    }       
    return user;        
}

}

这种例外可能有几个原因。首先,您需要验证是否在类定义之前正确插入了
@Service
注释。如果您的服务是UserService,则应遵循以下模式

@Service
public class UserService{

 // attributes and methods 

}

你能把自定义UserService的代码段放到这里吗?我已经在问题细节中添加了UserService类,请看一下。你也在自动连接UserDAO类。该类是否也使用@Repository或类似于类定义之前的注释?正如您描述的注释和配置一样,似乎没有任何错误。您的其他自动布线类是正常的,但只有一个特定的类。这有点令人困惑。顺便问一下,您如何尝试自动连接UserDetailService类?是的,其他自动连接工作正常…UserDetailService我自动连接为@Autowired UserService UserService;(我的自定义UserDetailService类名是UserService)注入自定义的
UserDetailsService
有什么意义?