繁体   English   中英

Spring Framework 错误创建具有名称的 bean 并且没有符合条件的 bean 类型

[英]Spring Framework Error creating bean with name and No qualifying bean of type

我开始使用 Spring Framework 并且遇到了一些问题:在我更改了@Service@Controller@Entity@Repository类的包结构后,我的项目停止了工作。 在更改之前我的项目运行正常,现在我不知道为什么它不会启动。

包结构类似于:

  • com.sandbox.config
  • com.sandbox.service
  • com.sandbox.service.impl
  • com.sandbox.controllers
  • com.sandbox.dao
  • com.sandbox.dao.impl
  • com.sandbox.entities

我改为:

  • com.sandbox.config
  • com.sandbox.business.service
  • com.sandbox.business.service.impl
  • com.sandbox.frontend.controllers
  • com.sandbox.persistency.dao
  • com.sandbox.persistency.dao.impl
  • com.sandbox.persistency.entities

我的配置类是:

SpringJdbcConfig:

package com.sandbox.config;
@Configuration
@ComponentScan(basePackages = { "com.sandbox.*" })
public class SpringJdbcConfig {...}

网络配置:

package com.sandbox.config;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.sandbox.*" })
public class WebConfig implements WebMvcConfigurer {...}

有谁知道我做错了什么? 我在 eclipse 输出控制台上收到这些错误:

  1. Error creating bean with name 'staffServiceImpl': Unsatisfied dependency expressed through field 'staffDao'.
  2. No qualifying bean of type 'com.sandbox.persistency.dao.StaffDao' available: expected at least 1 bean which qualifies as autowire candidate

对于我的项目的每个类,我都会收到相同的错误。

如下更改ComponentScan并重试:

@ComponentScan({ "com.sandbox.business.service","com.sandbox.persistency.dao" })

经过数小时的搜索和测试,我找到了解决问题的方法。 如果我更改包结构添加一个新的包级别,如果@ComponentScan多次使用, Spring 会复制 Bean 类 如果您将所有包保持在同一级别,则不会发生这种情况。 就我而言,我在SpringJdbcConfigWebConfig @ComponentScan中使用@ComponentScan

我是这样设置的:

SpringJdbcConfig:

@ComponentScan(basePackages = { "com.sandbox.*" })
public class SpringJdbcConfig {...}

网络配置:

@ComponentScan(basePackages = { "com.sandbox.*" })
public class WebConfig implements WebMvcConfigurer {...}

我把它改成:

SpringJdbcConfig:

@ComponentScan({ "com.sandbox.config", "com.sandbox.persistency.*" })
public class SpringJdbcConfig {...}

网络配置:

@ComponentScan({ "com.sandbox.config", "com.sandbox.business.*", "com.sandbox.persistency.*", "com.sandbox.frontend.*" })
public class WebConfig implements WebMvcConfigurer {...}

我希望它有帮助。 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM