英文:
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 '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)}
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<FilterEntity, String> {}
Service:
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public interface FilterService {
FilterResponseDto createFilter(FilterRequestDto filterRequestDto);
List<FilterResponseDto> 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("filters")
@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:
<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>
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:
- "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
答案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.
<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>
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:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.8</version>
<relativePath/>
</parent>
Step 2 - Dependency list
<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
@ServletComponentScan
@EnableJpaRepositories("package_with_my_repositories")
@EntityScan("package_with_my_entities")
@SpringBootApplication
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论