Java 原因:org.springframework.beans.factory.NoSuchBean定义异常:没有类型为的符合条件的bean

Java 原因:org.springframework.beans.factory.NoSuchBean定义异常:没有类型为的符合条件的bean,java,spring,spring-mvc,spring-security,autowired,Java,Spring,Spring Mvc,Spring Security,Autowired,我有一个自己无法处理的问题。(在我看来)我已经尽了一切可能。我真的需要你的帮助,因为我没有任何想法 我有一个错误: ... org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'securityConfig': Unsatisfied dependency expressed through field 'userService'; nested excep

我有一个自己无法处理的问题。(在我看来)我已经尽了一切可能。我真的需要你的帮助,因为我没有任何想法

我有一个错误:

...
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'securityConfig': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userService': Unsatisfied dependency expressed through field 'userRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'web.repositories.UserRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

...

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userService': Unsatisfied dependency expressed through field 'userRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'web.repositories.UserRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

...

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'web.repositories.UserRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

...
我的代码:

接口用户存储库

我的文件夹层次结构:

请帮忙。。。
几天来,我一直在尝试创建SpringSecurityCRUD应用程序。我很困惑。我无法解决此错误。

您必须启用JpaRepositories配置,将
@EnableJpaRepositories
注释添加到您的配置中

@Configuration
@EnableJpaRepositories("web.repositories")
public class ApplicationConfiguration {

   @Bean  public EntityManagerFactory entityManagerFactory() {
     // put here your favourite entity manager factory
   }
}

希望这有帮助

你能分享你的
@SpringBootApplication
注释类吗?@MichaelKreutz但我不使用sprinboot你需要一个@EnableJpaRepositories在你的spring配置中包含repos的packagename。否则您的回购将不会被spring容器获取。好的。有什么理由反对吗?@MichaelKreutz是的。在这个任务中,我不应该使用Springboot(非常感谢!感谢您的回复。这解决了我的问题。但还有另一个问题:由以下原因引起:org.spring frame.green bean.plant.nosuchbean定义异常:没有名为“entityManagerFactory”的bean不可用由您定义此类实体。特别是关于您对交易和bunc的要求是什么我建议你阅读参考资料
@Service("userService")
public class UserService implements UserDetailsService {

    @Autowired
    UserRepository userRepository;

    @Autowired
    RoleRepository roleRepository;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userRepository.findByUsername(username);

        if (user == null) {
            throw new UsernameNotFoundException("User not found");
        }

        return user;
    }

    public User findUserById(Long userId) {
        Optional<User> userFromDb = userRepository.findById(userId);
        return userFromDb.orElse(new User());
    }

    public List<User> allUsers() {
        return userRepository.findAll();
    }
    ...
}
@Configuration
@EnableWebSecurity
@ComponentScan(basePackages = "web")
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
@Qualifier("userService")
UserService userService;

    @Autowired
    protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userService).passwordEncoder(passwordEncoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin()
                .loginPage("/login")
                .successHandler(new LoginSuccessHandler())
                .loginProcessingUrl("/login")
                .usernameParameter("username")
                .passwordParameter("password")
                .permitAll();

        http.logout()
                .permitAll()
                .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                .logoutSuccessUrl("/login?logout")
                .and().csrf().disable();

        http
                .authorizeRequests()
                .antMatchers("/login").anonymous()
                .antMatchers("/admin_panel")
                .access("hasAnyRole('ADMIN')")
                .anyRequest()
                .authenticated()
        ;
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return NoOpPasswordEncoder.getInstance();
    }
}
@Configuration
@EnableJpaRepositories("web.repositories")
public class ApplicationConfiguration {

   @Bean  public EntityManagerFactory entityManagerFactory() {
     // put here your favourite entity manager factory
   }
}