org.springframework.beans.factory.UnsatisfiedDependencyException 错误创建 bean (repository)

huangapple go评论67阅读模式
英文:

org.springframework.beans.factory.UnsatisfiedDependencyException Error creating bean (repository)

问题

我正在尝试设置一个Spring Boot/MySQL/docker项目。在添加了存储库类之后,我开始遇到以下错误:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'filterController': Unsatisfied dependency expressed through field 'filterService':
Error creating bean with name 'filterServiceImpl': Unsatisfied dependency expressed through field 'filterRepository':
No qualifying bean of type 'backend.challenge.repositories.FilterRepository' available:
expected at least 1 bean which qualifies as autowire candidate. Dependency annotations:
{@org.springframework.beans.factory.annotation.Autowired(required=true)}

我检查了我的类和这个问题,对所有内容进行了注释。这里有两个存储库,我将只发布一个:

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface FilterRepository extends JpaRepository<FilterEntity, String> {}

服务:

import org.springframework.stereotype.Service;

import java.util.List;

@Service
public interface FilterService {
    FilterResponseDto createFilter(FilterRequestDto filterRequestDto);
    List<FilterResponseDto> getAllFilters();
}

服务实现:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class FilterServiceImpl implements FilterService{
    @Autowired
    private FilterRepository filterRepository;
    @Autowired
    private FilterChangelogRepository filterChangelogRepository;

    //方法实现在这里
}

控制器:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.util.List;

@RestController
@RequestMapping("filters")
@Validated
public class FilterController {
    @Autowired
    private FilterService filterService;
	//映射在这里
}

application.properties(指向在Docker上运行的MySQL数据库):

spring.datasource.url=jdbc:mysql://mysqldb:3306/challenge
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver

最后,我的依赖列表有些混乱:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>6.0.4</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>6.0.4</version>
    </dependency>
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>2.0.1.Final</version>
    </dependency>
    <dependency>
        <groupId>com.mysql</groupId>
        <artifactId>mysql-connector-j</artifactId>
        <version>8.0.32</version>
    </dependency>
    <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>javax.persistence-api</artifactId>
        <version>2.2</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>2.1.4.RELEASE</version>
    </dependency>
</dependencies>

如果有人能提供一些建议,我将不胜感激。

编辑1:清理了依赖项,仍然出现相同的错误。

编辑2:添加docker-compose文件:

services:

  yourapp:
    depends_on:
      - mysqldb
    restart: always
    build:
      context: ./
      dockerfile: Dockerfile
    ports:
      - "8080:8080"
    depends_on:
      mysqldb:
        condition: service_healthy
    environment:
      MYSQL_HOST: mysqldb
      MYSQL_USER: root
      MYSQL_PASSWORD: root
      MYSQL_PORT: 3306
    stdin_open: true
    tty: true

  mysqldb:
    image: mysql:8.0
    restart: always
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: challenge
      MYSQL_ROOT_PASSWORD: root
    healthcheck:
            test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
            timeout: 30s
            retries: 5
英文:

I'm trying to set up a Spring Boot/MySQL/docker project. After adding the repository classes, I started getting this error:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name &#39;filterController&#39;: Unsatisfied dependency expressed through field &#39;filterService&#39;: 
Error creating bean with name &#39;filterServiceImpl&#39;: Unsatisfied dependency expressed through field &#39;filterRepository&#39;: 
No qualifying bean of type &#39;backend.challenge.repositories.FilterRepository&#39; available: 
expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: 
{@org.springframework.beans.factory.annotation.Autowired(required=true)}

Checked my classes and this question, annotated everything.
There are two repositories, I'll just post one:

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface FilterRepository extends JpaRepository&lt;FilterEntity, String&gt; {}

Service:

import org.springframework.stereotype.Service;

import java.util.List;

@Service
public interface FilterService {
    FilterResponseDto createFilter(FilterRequestDto filterRequestDto);
    List&lt;FilterResponseDto&gt; getAllFilters();
}

Service implementation:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class FilterServiceImpl implements FilterService{
    @Autowired
    private FilterRepository filterRepository;
    @Autowired
    private FilterChangelogRepository filterChangelogRepository;

    //method implementations here
}

Controller:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.util.List;

@RestController
@RequestMapping(&quot;filters&quot;)
@Validated
public class FilterController {
    @Autowired
    private FilterService filterService;
	//mappings here
}

application.properties (pointing to the MySQL db running on docker)

spring.datasource.url=jdbc:mysql://mysqldb:3306/challenge
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver

Lastly, my dependency list which is quite a mess:

&lt;dependencies&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
	&lt;/dependency&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
		&lt;scope&gt;test&lt;/scope&gt;
	&lt;/dependency&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework&lt;/groupId&gt;
		&lt;artifactId&gt;spring-web&lt;/artifactId&gt;
		&lt;version&gt;6.0.4&lt;/version&gt;
	&lt;/dependency&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework&lt;/groupId&gt;
		&lt;artifactId&gt;spring-beans&lt;/artifactId&gt;
		&lt;version&gt;6.0.4&lt;/version&gt;
	&lt;/dependency&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;javax.validation&lt;/groupId&gt;
		&lt;artifactId&gt;validation-api&lt;/artifactId&gt;
		&lt;version&gt;2.0.1.Final&lt;/version&gt;
	&lt;/dependency&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;com.mysql&lt;/groupId&gt;
		&lt;artifactId&gt;mysql-connector-j&lt;/artifactId&gt;
		&lt;version&gt;8.0.32&lt;/version&gt;
	&lt;/dependency&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;javax.persistence&lt;/groupId&gt;
		&lt;artifactId&gt;javax.persistence-api&lt;/artifactId&gt;
		&lt;version&gt;2.2&lt;/version&gt;
	&lt;/dependency&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework.data&lt;/groupId&gt;
		&lt;artifactId&gt;spring-data-jpa&lt;/artifactId&gt;
		&lt;version&gt;2.1.4.RELEASE&lt;/version&gt;
	&lt;/dependency&gt;
&lt;/dependencies&gt;

Would appreciate if anybody could share some thoughts on this.

Edit1: cleaned dependencies, still getting the same error.

Edit2: Adding docker-compose file:

services:

  yourapp:
    depends_on:
      - mysqldb
    restart: always
    build:
      context: ./
      dockerfile: Dockerfile
    ports:
      - &quot;8080:8080&quot;
    depends_on:
      mysqldb:
        condition: service_healthy
    environment:
      MYSQL_HOST: mysqldb
      MYSQL_USER: root
      MYSQL_PASSWORD: root
      MYSQL_PORT: 3306
    stdin_open: true
    tty: true

  mysqldb:
    image: mysql:8.0
    restart: always
    ports:
      - &quot;3306:3306&quot;
    environment:
      MYSQL_DATABASE: challenge
      MYSQL_ROOT_PASSWORD: root
    healthcheck:
            test: [&quot;CMD&quot;, &quot;mysqladmin&quot; ,&quot;ping&quot;, &quot;-h&quot;, &quot;localhost&quot;]
            timeout: 30s
            retries: 5

答案1

得分: 0

我认为您可以使用以下方式替换您的依赖项。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
</dependency>
<dependency>
    <groupId>org.liquibase</groupId>
    <artifactId>liquibase-core</artifactId>
    <version>4.19.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

如果您在IDE内或作为非容器化应用程序运行Spring Boot应用程序,并且在与Docker引擎相同的主机上运行,请尝试使用 localhost 替代容器名称。

spring.datasource.url=jdbc:mysql://localhost:3306/challenge
英文:

I think you can replace your dependencies with the following.

&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
    &lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
    &lt;groupId&gt;javax.validation&lt;/groupId&gt;
    &lt;artifactId&gt;validation-api&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.liquibase&lt;/groupId&gt;
    &lt;artifactId&gt;liquibase-core&lt;/artifactId&gt;
    &lt;version&gt;4.19.0&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
    &lt;groupId&gt;mysql&lt;/groupId&gt;
    &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
    &lt;scope&gt;runtime&lt;/scope&gt;
&lt;/dependency&gt;

If you run your Spring Boot application inside your IDE or as a non-containerized application on the same host as your Docker engine, try using localhost instead of the container name.

spring.datasource.url=jdbc:mysql://localhost:3306/challenge

答案2

得分: 0

这里是您要翻译的内容:

Upon much trial and error and after fixing my dependencies as @KDW suggested, I finally figured out what was wrong.
经过多次试验和按照@KDW的建议修复我的依赖关系后,我终于找出了问题。

I created my project from Spring Initializr with version 3.0.2. Turns out there are some issues as mentioned here and here.
我使用版本3.0.2从Spring Initializr创建了我的项目。结果发现有一些问题,如这里这里所述。

Also, I was missing some annotations on my main class.
另外,我的主类缺少了一些注解。

Here's my solution:
以下是我的解决方案:

Step 1 - Downgrade Spring Boot version from 3.0.2 to 2.7.8:
步骤1 - 将Spring Boot版本从3.0.2降级为2.7.8:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.8</version>
    <relativePath/>
</parent>

Step 2 - Dependency list
步骤2 - 依赖列表

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.32</version>
    </dependency>
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>2.0.1.Final</version>
    </dependency>
    <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>javax.persistence-api</artifactId>
        <version>2.2</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>2.1.4.RELEASE</version>
    </dependency>
</dependencies>

Step 3 - Annotations on main class
步骤3 - 主类上的注解

@ServletComponentScan
@EnableJpaRepositories("package_with_my_repositories")
@EntityScan("package_with_my_entities")
@SpringBootApplication
英文:

Upon much trial and error and after fixing my dependencies as @KDW suggested, I finally figured out what was wrong.
I created my project from Spring Initializr with version 3.0.2. Turns out there are some issues as mentioned here and here.
Also, I was missing some annotations on my main class.
Here's my solution:

Step 1 - Downgrade Spring Boot version from 3.0.2 to 2.7.8:

&lt;parent&gt;
	&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
	&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
	&lt;version&gt;2.7.8&lt;/version&gt;
	&lt;relativePath/&gt;
&lt;/parent&gt;

Step 2 - Dependency list

&lt;dependencies&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
	&lt;/dependency&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
		&lt;scope&gt;test&lt;/scope&gt;
	&lt;/dependency&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter&lt;/artifactId&gt;
	&lt;/dependency&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
	&lt;/dependency&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;mysql&lt;/groupId&gt;
		&lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
		&lt;version&gt;8.0.32&lt;/version&gt;
	&lt;/dependency&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;javax.validation&lt;/groupId&gt;
		&lt;artifactId&gt;validation-api&lt;/artifactId&gt;
		&lt;version&gt;2.0.1.Final&lt;/version&gt;
	&lt;/dependency&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;javax.persistence&lt;/groupId&gt;
		&lt;artifactId&gt;javax.persistence-api&lt;/artifactId&gt;
		&lt;version&gt;2.2&lt;/version&gt;
	&lt;/dependency&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework.data&lt;/groupId&gt;
		&lt;artifactId&gt;spring-data-jpa&lt;/artifactId&gt;
		&lt;version&gt;2.1.4.RELEASE&lt;/version&gt;
	&lt;/dependency&gt;
&lt;/dependencies&gt;

Step 3 - Annotations on main class

@ServletComponentScan
@EnableJpaRepositories(&quot;package_with_my_repositories&quot;)
@EntityScan(&quot;package_with_my_entities&quot;)
@SpringBootApplication

huangapple
  • 本文由 发表于 2023年2月19日 04:02:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75496058.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定