【问题标题】:UnsupportedDependencyException when running my spring boot application运行我的 Spring Boot 应用程序时出现 UnsupportedDependencyException
【发布时间】:2020-09-01 01:05:07
【问题描述】:

当我运行我的 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。有谁知道如何解决这个问题?谢谢。

【问题讨论】:

  • 您排除了数据源的自动配置。请问你能添加你的@Configuration 吗?
  • application.properties spring.datasource.url=jdbc:mysql//localhost:3306/laptop_db spring.datasource.username=root #spring.datasource.password=root spring.jpa-show-sql= true spring.jpa.properties.hibernate.format_sql=true server.port=8080
  • 这里缺少一些部分。你能在 git 或tmpfiles.org 上分享你的项目吗?我会尝试在我身边运行它。

标签: java spring spring-boot


【解决方案1】:

为 LaptopRepository 添加类级别注释,例如:@Repository ... 以便应用程序上下文可以为您创建 bean 对象。

【讨论】:

    【解决方案2】:

    需要自己配置DataSource或者去掉DataSourceAutoConfiguration的排除。如果没有数据源,则不会创建存储库。

    // 1) remove the annotation, Spring will configure the datasource
    //@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
    
    // 2) configure by youself if you need
    @ConfigurationProperties("spring.datasource")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }
    

    Documentation

    【讨论】:

    • 我设法让它在没有数据源注释的情况下工作。诀窍在于 application.properties。
    【解决方案3】:

    诀窍在于“application.properties”,您需要在其中指定数据源凭据。

    spring.datasource.url=jdbc:mysql://localhost:3306/laptop_db
    spring.datasource.username=root
    spring.datasource.password=
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
    spring.jpa.show-sql=true
    spring.jpa.hibernate.ddl-auto=update
    server.port=8765
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-18
      • 1970-01-01
      • 2017-02-18
      • 1970-01-01
      • 2018-08-25
      相关资源
      最近更新 更多