java - 运行我的 Spring Boot 应用程序时出现 UnsupportedDependencyException
问题描述
当我运行我的 spring 应用程序时,我得到一个 UnsupportedDependencyException,即使我自动装配了我的 bean 并定义了我的 applications.properties。
例外:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field laptopRepository in com.microcenter.service.LaptopServiceImpl required a bean of type 'com.microcenter.repository.LaptopRepository' 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 'com.microcenter.repository.LaptopRepository' in your configuration.
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'laptopApi': Unsatisfied dependency expressed through field 'laptopService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'laptopService': Unsatisfied dependency expressed through field 'laptopRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.microcenter.repository.LaptopRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1420)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:897)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
at com.microcenter.MicroCenterApplication.main(MicroCenterApplication.java:15)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'laptopService': Unsatisfied dependency expressed through field 'laptopRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.microcenter.repository.LaptopRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1420)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1307)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1227)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640)
... 20 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.microcenter.repository.LaptopRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1717)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1273)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1227)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640)
... 33 more
主类:
package com.microcenter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@SpringBootApplication
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class MicroCenterApplication {
public static void main(String[] args) {
try {
SpringApplication.run(MicroCenterApplication.class, args);
} catch (Throwable e) {
e.printStackTrace();
}
}
}
API类:
@RestController
@RequestMapping("/laptops")
public class LaptopApi
{
@Autowired
private LaptopService laptopService;
@Autowired
private Environment environment;
@GetMapping("/")
public ResponseEntity<List<LaptopDTO>> getLaptops()
{
List<LaptopDTO> laptops = laptopService.getLaptops();
return new ResponseEntity<>(laptops, HttpStatus.OK);
}
}
存储库类:
package com.microcenter.repository;
import com.microcenter.entity.Laptop;
import org.springframework.data.repository.CrudRepository;
public interface LaptopRepository extends CrudRepository<Laptop, Integer>
{
}
我已经自动装配了所有必要的 bean,但是,它说它找不到所需的 bean。有谁知道如何解决这个问题?谢谢。
解决方案
为 LaptopRepository ex 添加类级别注释:@Repository ... 以便应用程序上下文可以为您创建 bean 对象。
推荐阅读
- php - 两个模型的通用 model_id - Laravel 5 - Eloquent
- python-3.x - ZONE_RESOURCE_POOL_EXHAUSTED 是 googleapiclient.errors.httpError 吗?
- node.js - (节点:11254)UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝 id:1):TypeError:无法读取 null 的属性“长度”
- c# - 当 Response.ContentType 为“audio/wav”时,如何修复谷歌浏览器加载页面两次?
- node.js - 如何正确使用 Nest.js 的 @Headers?
- firefox - Angular Material Table Firefox Mat Row 单击事件,在 Mat Cell 中单击按钮
- amazon-dynamodb - AppSync 解析器中的 DynamoDB TransactWriteItems
- r - 将具有不同单位的期限(时间段)的特征向量添加到日期
- javascript - CSS 样式表未正确连接到 ejs 文件
- java - 在字节伙伴的 MethodDelegation 中有效地获取调用者