BeanCreationException:自动装配的依赖项因另一个 Maven spring 项目而失败
BeanCreationException: autowired dependencies failed with another maven spring project
我在将自动装配与第二个 spring 引导项目结合使用时遇到了一些问题。希望你能给我一些提示来解决这个问题。
项目设置:
项目数据中心管理数据存储访问。项目数据集成做一些其他的事情,但需要项目数据中心与数据存储进行通信。两个项目都基于 spring boot,并作为 maven 项目实现。
问题:
当我尝试启动数据集成项目 (运行 Application.java) 时,其中数据集线器项目是通过 Maven 依赖项集成的,我在线程末尾遇到了异常。项目数据中心作为独立的演示应用程序工作。它还与 spring 引导 Web 应用程序(通过 HTTP 请求读取数据存储实体)一起工作(通过 pom.xml 作为依赖项集成)。
有什么想法吗???最令人沮丧的一点是,当项目数据集成作为 Web 应用程序实现时,相同的设置仍然有效。但是当数据集成作为本机 java 应用程序 (jar) 实现时它不起作用。
数据中心 (com.example.mcp.datahub):
Application.java
package com.example.mcp.datahub;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.example.mcp.datahub.model.WorkingDay;
import com.example.mcp.datahub.service.WorkingDayService;
@SpringBootApplication
@EnableTransactionManagement
@ComponentScan("com.example.mcp")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
WorkingDayRepository.java
package com.example.mcp.datahub.repositories;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.example.mcp.datahub.model.WorkingDay;
@Repository
public interface WorkingDayRepository extends CrudRepository<WorkingDay, Long> {
}
WorkingDayService.java
package com.example.mcp.datahub.service;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.example.mcp.datahub.model.WorkingDay;
import com.example.mcp.datahub.repositories.WorkingDayRepository;
@Service
@ComponentScan("com.example.mcp")
public class WorkingDayService {
@Autowired
public WorkingDayRepository workingDayRepository;
@Transactional(readOnly=true)
public List<WorkingDay> getWorkingDays() {
List<WorkingDay> list = (List<WorkingDay>) workingDayRepository.findAll();
return list;
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.mcp.datahub</groupId>
<artifactId>com.example.mcp.datahub</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>com.example.mcp.datahub</name>
<description>Data Hub</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
数据集成(com.example.mcp.dataintegration):
Application.java
package com.example.mcp.dataintegration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
@ComponentScan("com.example.mcp")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
ScheduledTasks.java
package com.example.mcp.dataintegration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import com.example.mcp.datahub.model.WorkingDay;
import com.example.mcp.datahub.service.WorkingDayService;
@Service
@ComponentScan("com.example.mcp")
public class ScheduledTasks {
@Autowired
public WorkingDayService workingDayService;
@Scheduled(fixedDelay = 5000)
public void reportCurrentTime() {
//do something with workingDayService
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.mcp.dataintegration</groupId>
<artifactId>com.example.mcp.dataintegration</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>com.example.mcp.dataintegration</name>
<description>Data Integration Layer</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.version>
</properties>
<dependencies>
<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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.example.mcp.datahub</groupId>
<artifactId>com.example.mcp.datahub</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
异常
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scheduledTasks': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.example.mcp.datahub.service.WorkingDayService com.example.mcp.dataintegration.ScheduledTasks.workingDayService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'workingDayService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.example.mcp.datahub.repositories.WorkingDayRepository com.example.mcp.datahub.service.WorkingDayService.workingDayRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.mcp.datahub.repositories.WorkingDayRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
...
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at com.example.mcp.dataintegration.Application.main(Application.java:15) [classes/:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.example.mcp.datahub.service.WorkingDayService com.example.mcp.dataintegration.ScheduledTasks.workingDayService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'workingDayService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.example.mcp.datahub.repositories.WorkingDayRepository com.example.mcp.datahub.service.WorkingDayService.workingDayRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.mcp.datahub.repositories.WorkingDayRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
... 16 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'workingDayService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.example.mcp.datahub.repositories.WorkingDayRepository com.example.mcp.datahub.service.WorkingDayService.workingDayRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.mcp.datahub.repositories.WorkingDayRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
...
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
... 18 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.example.mcp.datahub.repositories.WorkingDayRepository com.example.mcp.datahub.service.WorkingDayService.workingDayRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.mcp.datahub.repositories.WorkingDayRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
... 29 common frames omitted
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.mcp.datahub.repositories.WorkingDayRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
...
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
... 31 common frames omitted
您是否尝试过 @EnableJpaRepositories 在应用程序 class 中使用 @Configuration 运行。
我已经在我的一个项目中尝试过类似的方法,它与你的项目很相似并且对我有用;
@Configuration
@EnableJpaRepositories("com.example.mcp")
@EntityScan("com.example.mcp")
WorkingDay 有 @Entity 注释。
我已经解决了问题。以下 post 很有帮助:Spring Boot And Multi-Module Maven Projects
我将文件 com.example.mcp.dataintegration.Application.java 移到了 com.example.mcp.Application.java。但更不清楚为什么我的 ComponentScan amd JPARepo 定义被忽略了...
我在将自动装配与第二个 spring 引导项目结合使用时遇到了一些问题。希望你能给我一些提示来解决这个问题。
项目设置: 项目数据中心管理数据存储访问。项目数据集成做一些其他的事情,但需要项目数据中心与数据存储进行通信。两个项目都基于 spring boot,并作为 maven 项目实现。
问题: 当我尝试启动数据集成项目 (运行 Application.java) 时,其中数据集线器项目是通过 Maven 依赖项集成的,我在线程末尾遇到了异常。项目数据中心作为独立的演示应用程序工作。它还与 spring 引导 Web 应用程序(通过 HTTP 请求读取数据存储实体)一起工作(通过 pom.xml 作为依赖项集成)。
有什么想法吗???最令人沮丧的一点是,当项目数据集成作为 Web 应用程序实现时,相同的设置仍然有效。但是当数据集成作为本机 java 应用程序 (jar) 实现时它不起作用。
数据中心 (com.example.mcp.datahub):
Application.java
package com.example.mcp.datahub;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.example.mcp.datahub.model.WorkingDay;
import com.example.mcp.datahub.service.WorkingDayService;
@SpringBootApplication
@EnableTransactionManagement
@ComponentScan("com.example.mcp")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
WorkingDayRepository.java
package com.example.mcp.datahub.repositories;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.example.mcp.datahub.model.WorkingDay;
@Repository
public interface WorkingDayRepository extends CrudRepository<WorkingDay, Long> {
}
WorkingDayService.java
package com.example.mcp.datahub.service;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.example.mcp.datahub.model.WorkingDay;
import com.example.mcp.datahub.repositories.WorkingDayRepository;
@Service
@ComponentScan("com.example.mcp")
public class WorkingDayService {
@Autowired
public WorkingDayRepository workingDayRepository;
@Transactional(readOnly=true)
public List<WorkingDay> getWorkingDays() {
List<WorkingDay> list = (List<WorkingDay>) workingDayRepository.findAll();
return list;
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.mcp.datahub</groupId>
<artifactId>com.example.mcp.datahub</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>com.example.mcp.datahub</name>
<description>Data Hub</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
数据集成(com.example.mcp.dataintegration):
Application.java
package com.example.mcp.dataintegration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
@ComponentScan("com.example.mcp")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
ScheduledTasks.java
package com.example.mcp.dataintegration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import com.example.mcp.datahub.model.WorkingDay;
import com.example.mcp.datahub.service.WorkingDayService;
@Service
@ComponentScan("com.example.mcp")
public class ScheduledTasks {
@Autowired
public WorkingDayService workingDayService;
@Scheduled(fixedDelay = 5000)
public void reportCurrentTime() {
//do something with workingDayService
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.mcp.dataintegration</groupId>
<artifactId>com.example.mcp.dataintegration</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>com.example.mcp.dataintegration</name>
<description>Data Integration Layer</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.version>
</properties>
<dependencies>
<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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.example.mcp.datahub</groupId>
<artifactId>com.example.mcp.datahub</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
异常
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scheduledTasks': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.example.mcp.datahub.service.WorkingDayService com.example.mcp.dataintegration.ScheduledTasks.workingDayService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'workingDayService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.example.mcp.datahub.repositories.WorkingDayRepository com.example.mcp.datahub.service.WorkingDayService.workingDayRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.mcp.datahub.repositories.WorkingDayRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
...
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at com.example.mcp.dataintegration.Application.main(Application.java:15) [classes/:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.example.mcp.datahub.service.WorkingDayService com.example.mcp.dataintegration.ScheduledTasks.workingDayService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'workingDayService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.example.mcp.datahub.repositories.WorkingDayRepository com.example.mcp.datahub.service.WorkingDayService.workingDayRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.mcp.datahub.repositories.WorkingDayRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
... 16 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'workingDayService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.example.mcp.datahub.repositories.WorkingDayRepository com.example.mcp.datahub.service.WorkingDayService.workingDayRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.mcp.datahub.repositories.WorkingDayRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
...
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
... 18 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.example.mcp.datahub.repositories.WorkingDayRepository com.example.mcp.datahub.service.WorkingDayService.workingDayRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.mcp.datahub.repositories.WorkingDayRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
... 29 common frames omitted
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.mcp.datahub.repositories.WorkingDayRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
...
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
... 31 common frames omitted
您是否尝试过 @EnableJpaRepositories 在应用程序 class 中使用 @Configuration 运行。
我已经在我的一个项目中尝试过类似的方法,它与你的项目很相似并且对我有用;
@Configuration
@EnableJpaRepositories("com.example.mcp")
@EntityScan("com.example.mcp")
WorkingDay 有 @Entity 注释。
我已经解决了问题。以下 post 很有帮助:Spring Boot And Multi-Module Maven Projects
我将文件 com.example.mcp.dataintegration.Application.java 移到了 com.example.mcp.Application.java。但更不清楚为什么我的 ComponentScan amd JPARepo 定义被忽略了...