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

前提是你 提交于 2021-02-06 10:17:31

问题


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?


回答1:


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).




回答2:


Annotatate your class implementing UserRepository with spring @Component annotation

@Component
public MyUserRepository implements UserRepository
{
    User findByEmail(String email){
            return new User();
    }
}



回答3:


[org.cru.cloud.management.data.UserRepository]: : No qualifying bean of type

Can not find UserRepository bean. Maybe you forgot to put annotation to UserRepository.

@Component
public MyClass implements UserRepository{}

if you autowire userRepository then you MyClass will be injected.

Also :

public MyClass implements UserRepository{}
public YourClass implements UserRepository{}

@Autowired
public blabla(@Qualifier("myClass") UserRepository userRepo)

@Autowired
public blabla(@Qualifier("yourClass") UserRepository userRepo)

I guess this will help.




回答4:


It seems you are using @EnableJpaRepositories to configure the database repository. It has two option load beans, the following one method you can use.

@EnableJpaRepositories(basePackages={"com.test.repo"})

or

@EnableJpaRepositories(basePackageClasses=TestRepo.class)


来源:https://stackoverflow.com/questions/26577084/spring-unsatisfied-dependency-expressed-through-constructor-argument-with-index

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!