Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Unsatisfied dependency expressed through constructor argument with index 0 of type

The full message is

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: 
    Error creating bean with name 'userRepositoryUserDetailsService' defined in file 
    [/Users/tdaley/apache-tomcat-8.0.12/wtpwebapps/cloud-management/WEB-INF/classes/
       org/cru/cloud/management/security/UserRepositoryUserDetailsService.class]: 
    Unsatisfied dependency expressed through constructor argument with index 0 of type 
[org.cru.cloud.management.data.UserRepository]: : No qualifying bean of 
    type [org.cru.cloud.management.data.UserRepository] found for dependency: 
        expected at least 1 bean which qualifies 
        as autowire candidate for this dependency. 
     Dependency annotations: {}; 
nested exception is 
    org.springframework.beans.factory.NoSuchBeanDefinitionException: 
       No qualifying bean of type [org.cru.cloud.management.data.UserRepository] found 
       for dependency: expected at least 1 bean 
       which qualifies as autowire candidate for this dependency. 
     Dependency annotations: {}

I'm using Java configuration. The following code segments are from the spring boot tutorial gs-spring-security-3.2:

package org.cru.cloud.management.security;

import java.util.Collection;

import org.cru.cloud.management.data.User;
import org.cru.cloud.management.data.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class UserRepositoryUserDetailsService implements UserDetailsService {
    private final UserRepository userRepository;

    @Autowired
    public UserRepositoryUserDetailsService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @Override
    public UserDetails loadUserByUsername(String username)
            throws UsernameNotFoundException {
        User user = userRepository.findByEmail(username);
        if(user == null) {
            throw new UsernameNotFoundException("Could not find user " + username);
        }
        return new UserRepositoryUserDetails(user);
    }

    private final static class UserRepositoryUserDetails extends User implements UserDetails {    

        private UserRepositoryUserDetails(User user) {
            super(user);
        }

        @Override
        public Collection<? extends GrantedAuthority> getAuthorities() {
            return AuthorityUtils.createAuthorityList("ROLE_USER");
        }

        @Override
        public String getUsername() {
            return getEmail();
        }

        @Override
        public boolean isAccountNonExpired() {
            return true;
        }

        @Override
        public boolean isAccountNonLocked() {
            return true;
        }

        @Override
        public boolean isCredentialsNonExpired() {
            return true;
        }

        @Override
        public boolean isEnabled() {
            return true;
        }

        private static final long serialVersionUID = 5639683223516504866L;
    }
}

public interface UserRepository extends CrudRepository<User, Long>
{
    User findByEmail(String email);
}

Suggestions on how to fix this?

like image 753
AixNPanes Avatar asked Oct 26 '14 19:10

AixNPanes


1 Answers

Do you have the @EnableJpaRepositories annotation somewhere in your config classes and is the scan set up correctly? By default the package in which the annotated class or interface exists becomes the base package for repository scan. You can control that by setting a value to the basePackages (or even better - basePackageClasses) property.

You could use Spring Tools Suite to check, what beans are declared in your context (Repositories should be visible under Spring Elements > Beans > Spring Data Repositories).

like image 109
Apokralipsa Avatar answered Nov 15 '22 21:11

Apokralipsa