我在自動裝配和第二個彈簧引導項目的組合中遇到了一些問題。希望你能給我一些提示來解決這個問題。BeanCreationException:自動裝配依賴關係失敗,另一個Maven彈簧項目
項目設置: 項目datahub管理數據存儲訪問。項目dataintegration做了一些其他的東西,但需要項目datahub與數據存儲交流。這兩個項目都基於Spring引導,並被實現爲maven項目。
問題: 當我嘗試啓動dataintegration項目(運行Application.java),其中datahub項目通過maven的依賴集成,我得到在線程結束除外。項目datahub作爲獨立的演示應用程序工作。它也可以與spring引導web應用程序(通過HTTP請求讀取數據存儲實體)一起工作(通過pom.xml作爲依賴關係進行集成)。
任何想法???最令人沮喪的是,當項目數據整合作爲Web應用程序實現時,相同的設置正在工作。但是當dataintegration被實現爲原生Java應用程序(jar)時,它不起作用。
Datahub(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>
Dataintegration(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
讓我猜測,數據集成沒有'spring-boot-starter-data-jpa'作爲依賴項。另外,請刪除服務類中的@ @ ComponentScan,因爲這只是無用的,它只能在'@ Configuration'類中使用。 –
@ m-deinum:不幸的是,我忘記了第二個pom.xml。已連接。 spring-boot-starter-data-jpa作爲依賴項提供。 –