【问题标题】:No qualifying bean of type available - expected at least 1 bean which qualifies as autowire candidate没有可用类型的合格 bean - 预计至少有 1 个有资格作为自动装配候选者的 bean
【发布时间】:2021-01-13 15:52:31
【问题描述】:

我大致遵循 Spring In Action,第 5 版中的代码。目前遇到自动装配 bean 的问题,我似乎无法弄清楚为什么 bean 在运行上下文中不可用。以下是一些相关的类:

需要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";
    }

}

UserRepository JPA 接口:

package tacos.data.jpa;

import org.springframework.data.repository.CrudRepository;
import tacos.security.User;

public interface UserRepository extends CrudRepository<User, Long> {
}

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;
    }

}

主类:

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");
    }
}

错误本身:

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

不太清楚为什么UserRepository bean 没有正确连接。我的项目中有其他类,其中 bean 接线正确发生。

很高兴包含任何其他需要了解我为什么会看到此错误的类。

【问题讨论】:

  • 您是否在 sprint 配置中启用了存储库扫描?
  • 请分享您的配置。魔鬼在细节中。
  • @SSC 存储库扫描应该启用,因为我的 main 方法的类上面有 @SpringBootAnnotation,它应该扫描所有子包中的组件。
  • @yousafsajjad 很高兴分享它。在这种情况下,您指的是哪个配置?
  • 我指的是datasourceConfig,您可能在其中添加了注释:EnableJpaRepositories ComponentScan

标签: java spring spring-boot


【解决方案1】:

尝试以下几点

  1. 请检查 Spring Boot 是否能够扫描您的存储库类的包结构。
  2. 还要检查您的 User 类是否应使用 @Entity 进行注释。

【讨论】:

  • 我在帖子中添加了User 和主类。 User 类确实有@Entity 注解,主类注解有@SpringBootApplication 这应该意味着任何子包中的所有组件都应该被Spring Boot 拾取。
  • @Pastafarian 尝试在你的控制器中自动装配 UserRepository
  • 从 Spring 4.3 开始,您不再需要 @Autowired 注释来注入 bean。构造函数注入现在是首选方法。见link
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-25
  • 2020-05-31
  • 2021-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多