【问题标题】:Required a bean of type that could not be found需要一个找不到类型的 bean
【发布时间】:2020-08-04 17:56:13
【问题描述】:

我在我的项目中做了一些休息 api。我有他们的模型、存储库、控制器和服务。他们工作正常,我的 sql server 也成功创建了所有表。

现在我正在尝试为我的 api 实现基于角色的授权,为此我创建了一个 usersauth 和一个 role 模型。这是我正在关注的代码:

https://github.com/TechPrimers/spring-security-db-example

Configuration.java:

//import statements
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableJpaRepositories(basePackageClasses = UsersAuthRepository.class)
@EnableWebSecurity
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

Application.java:

  @SpringBootApplication


 public class Application {

public static void main(String[] args) {
          SpringApplication.run(Application.class, args);
    }
 
}

当我运行我的项目时,它给了我这个错误:

APPLICATION FAILED TO START
***************************

Description:

Field BlankRepository in 

required a bean of type 


that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 
'BlankRepository' in 
 your 
 configuration.

现在这是我之前在实施授权之前创建的模型之一。该表也存在于 sql server 中。实施授权后出现此错误。我也有这个所需的存储库。 我是spring boot的新手,但我认为这个错误与

  @EnableJpaRepositories(basePackageClasses = UsersRepository.class)

在我的文件中。也许我的应用程序没有阅读那些回购或其他东西。我仍然不太明白这条线背后的目的,所以任何人都可以帮助我我错过了什么吗?汉克斯!

【问题讨论】:

  • 只需使用@EnableJpaRepositories。当您使用 basePackageClasses 时,它仅启用特定的 Repository 类。
  • @SKumar 我试过只使用注释来这样做,但它仍然给我同样的错误
  • SecurityConfiguration.java 的包名是什么?
  • 尝试将 @EnableJpaRepositories 移动到 SystemyconfigurationApplication
  • @SKumar 它是package risknucleus.systemyconfiguration.rolebasedauth;

标签: java spring-boot visual-studio-code spring-repositories


【解决方案1】:

添加注释

@SpringBootApplication(scanBasePackages={xxx})

@Repository
public interface UsersRepository extends JpaRepository<Users, Integer> {
    Optional<Users> findByName(String username);
}

【讨论】:

  • 我要在 scanBasePackages 中添加什么?我有很多存储库将近 80
猜你喜欢
  • 2018-02-05
  • 2017-09-17
  • 2020-07-06
  • 2021-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-11
  • 1970-01-01
相关资源
最近更新 更多