Spring不满意的依赖关系通过构造函数参数表示,索引为0

时间:2014-10-26 19:37:20

标签: java spring spring-mvc

完整的信息是

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: {}

我正在使用Java配置。以下代码段来自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);
}

有关如何解决此问题的建议?

5 个答案:

答案 0 :(得分:9)

您的配置类中是否有@EnableJpaRepositories注释并且扫描设置正确?默认情况下,带注释的类或接口所在的包成为存储库扫描的基础包。您可以通过将值设置为basePackages(甚至更好 - basePackageClasses)属性来控制它。

您可以使用Spring Tools Suite检查在您的上下文中声明了哪些bean(存储库应该在Spring Elements&gt; Beans&gt; Spring Data Repositories下可见)。

答案 1 :(得分:0)

[org.cru.cloud.management.data.UserRepository] ​​::没有类型的合格bean

找不到UserRepository bean。也许你忘了把注释放到UserRepository。

@Component
public MyClass implements UserRepository{}

如果你自动装配userRepository,那么你将注入MyClass。

另外:

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

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

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

我想这会有所帮助。

答案 2 :(得分:0)

使用spring UserRepository注释

注释实现@Component的类
@Component
public MyUserRepository implements UserRepository
{
    User findByEmail(String email){
            return new User();
    }
}

答案 3 :(得分:-1)

您应该使用@Repository注释对实现UserRepository的类进行注释。

答案 4 :(得分:-2)

您似乎正在使用@EnableJpaRepositories来配置数据库存储库。它有两个选项加载bean,您可以使用以下一种方法。

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

@EnableJpaRepositories(basePackageClasses=TestRepo.class)