Java Spring Unsatisfied 依赖项通过构造函数参数表示,类型为索引 0
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26577084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Unsatisfied dependency expressed through constructor argument with index 0 of type
提问by AixNPanes
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:
我正在使用 Java 配置。以下代码段来自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?
关于如何解决这个问题的建议?
回答by Gurkan ?lleez
[org.cru.cloud.management.data.UserRepository]: : No qualifying bean of type
[org.cru.cloud.management.data.UserRepository]:: 没有符合条件的 bean 类型
Can not find UserRepository bean. Maybe you forgot to put annotation to UserRepository.
找不到 UserRepository bean。也许你忘了给 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.
我想这会有所帮助。
回答by Apokralipsa
Do you have the @EnableJpaRepositoriesannotation 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.
您的配置类中是否有@EnableJpaRepositories注释并且扫描设置是否正确?默认情况下,包含注释类或接口的包成为存储库扫描的基本包。您可以通过为 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 下可见)。
回答by Andrés Oviedo
Annotatate your class implementing UserRepository
with spring @Component
annotation
UserRepository
使用 spring@Component
注释注释您的类实现
@Component
public MyUserRepository implements UserRepository
{
User findByEmail(String email){
return new User();
}
}
回答by Kumar
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
配置数据库存储库。它有两个选项加载 bean,您可以使用以下一种方法。
@EnableJpaRepositories(basePackages={"com.test.repo"})
or
或者
@EnableJpaRepositories(basePackageClasses=TestRepo.class)