[英]No qualifying bean of type available - expected at least 1 bean which qualifies as autowire candidate
I'm roughly following the code in Spring In Action, 5th ed.我大致遵循 Spring In Action,第 5 版中的代码。 Currently running into a problem autowiring a bean, and I can't seem to figure out why the bean isn't available in the running context.目前在自动装配 bean 时遇到问题,我似乎无法弄清楚为什么 bean 在运行上下文中不可用。 Here are some of the relevant classes:以下是一些相关的类:
Controller where the UserRepository
bean is expected:需要UserRepository
bean 的控制器:
package tacos.web;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import tacos.data.jpa.UserRepository;
import tacos.security.RegistrationForm;
@Controller
@RequestMapping("/register")
public class RegistrationController {
private final UserRepository userRepo;
private final PasswordEncoder passwordEncoder;
public RegistrationController(
UserRepository userRepo, PasswordEncoder passwordEncoder) {
this.userRepo = userRepo;
this.passwordEncoder = passwordEncoder;
}
@GetMapping
public String registerForm() {
return "registration";
}
@PostMapping
public String processRegistration(RegistrationForm form) {
userRepo.save(form.toUser(passwordEncoder));
return "redirect:/login";
}
}
The UserRepository
JPA interface: UserRepository
JPA 接口:
package tacos.data.jpa;
import org.springframework.data.repository.CrudRepository;
import tacos.security.User;
public interface UserRepository extends CrudRepository<User, Long> {
}
The User
class: User
类:
package tacos.security;
import java.util.Arrays;
import java.util.Collection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import lombok.AccessLevel;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
@Entity
@Data
@NoArgsConstructor(access=AccessLevel.PRIVATE, force=true)
@RequiredArgsConstructor
public class User implements UserDetails {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private final String username;
private final String password;
private final String fullname;
private final String street;
private final String city;
private final String state;
private final String zip;
private final String phoneNumber;
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return Arrays.asList(new SimpleGrantedAuthority("USER"));
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
}
The main class:主要类:
package tacos;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@SpringBootApplication
public class TacoCloudApplication implements WebMvcConfigurer {
public static void main(String[] args) {
SpringApplication.run(TacoCloudApplication.class, args);
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("home");
}
}
The error itself:错误本身:
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'registrationController' defined in file: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'tacos.data.jpa.UserRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
Not quite sure why the UserRepository
bean isn't being wired correctly.不太确定为什么UserRepository
bean 没有正确连接。 I have other classes in my project where the bean wiring happens correctly.我的项目中有其他类,其中 bean 接线正确发生。
Happy to include any other classes needed to understand why I'm seeing this error.很高兴包含理解为什么我看到此错误所需的任何其他类。
Try below points尝试以下几点
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.