简体   繁体   English

Spring Unsatisfied 依赖项通过构造函数参数表示,类型为索引 0

[英]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.我正在使用 Java 配置。 The following code segments are from the spring boot tutorial gs-spring-security-3.2:以下代码段来自spring boot教程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?关于如何解决这个问题的建议?

Do you have the @EnableJpaRepositories annotation somewhere in your config classes and is the scan set up correctly?您的配置类中是否有@EnableJpaRepositories注释并且扫描设置是否正确? 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.您可以通过为 basePackages(或者甚至更好 - basePackageClasses)属性设置一个值来控制它。

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).您可以使用 Spring Tools Suite 来检查在您的上下文中声明了哪些 bean(存储库应该在 Spring Elements > Beans > Spring Data Repositories 下可见)。

[org.cru.cloud.management.data.UserRepository]: : No qualifying bean of type [org.cru.cloud.management.data.UserRepository]:: 没有符合条件的 bean 类型

Can not find UserRepository bean.找不到 UserRepository bean。 Maybe you forgot to put annotation to UserRepository.也许你忘了给 UserRepository 加注解。

@Component
public MyClass implements UserRepository{}

if you autowire userRepository then you MyClass will be injected.如果您自动装配 userRepository,那么您的 MyClass 将被注入。

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.我想这会有所帮助。

Annotatate your class implementing UserRepository with spring @Component annotation使用 spring @Component注释对实现UserRepository @Component注释

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

It seems you are using @EnableJpaRepositories to configure the database repository.您似乎正在使用@EnableJpaRepositories来配置数据库存储库。 It has two option load beans, the following one method you can use.它有两个选项加载 bean,您可以使用以下一种方法。

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

or或者

@EnableJpaRepositories(basePackageClasses=TestRepo.class)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过索引为0的构造函数参数表示不满意的依赖关系 - Unsatisfied dependency expressed through constructor argument with index 0 通过具有类型3的索引3的构造函数参数表示的不满意依赖性构造函数参数类型 - Unsatisfied dependency expressed through constructor argument with index 3 of type Ambiguous constructor argument types 通过构造函数参数(类型为0的索引0)表达不满意的依赖关系-&gt;没有类型为合格的bean - Unsatisfied dependency expressed through constructor argument with index 0 of type -> No qualifying bean of type 通过构造函数参数表示不满足的依赖关系,类型为 [java.lang.Class] 的索引为 0 - Unsatisfied dependency expressed through constructor argument with index 0 of type [java.lang.Class] org.springframework.beans.factory.UnsatisfiedDependencyException:不满意的依赖关系通过索引为0的构造函数参数表示 - org.springframework.beans.factory.UnsatisfiedDependencyException: Unsatisfied dependency expressed through constructor argument with index 0 通过带有索引的构造函数参数表示的不满意的依赖关系,但是已经为这些bean提供了对其他bean的引用 - Unsatisfied dependency expressed through constructor argument with index, but the beans are already provided references to other bean 通过构造函数参数0表示的不满意的依赖:没有类型的限定bean - Spring引导 - Unsatisfied dependency expressed through constructor parameter 0: No qualifying bean of type - Spring boot 通过构造函数参数 2 表示的不满足的依赖关系 - Unsatisfied dependency expressed through constructor parameter 2 通过构造函数参数 0 表示的不满足的依赖关系 - Unsatisfied dependency expressed through constructor parameter 0 通过构造函数参数表示的未满足的依赖关系 - Unsatisfied dependency expressed through constructor parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM