BeanCreationException:自动连接的依赖项与另一个maven spring项目失败

时间:2016-07-08 08:41:44

标签: java spring maven jar autowired

我遇到了与第二个春季启动项目相结合的自动装配问题。希望你能给我一些解决问题的提示。

项目设置: 项目datahub管理数据存储访问。项目dataintegration做了一些其他的东西,但需要项目datahub与数据存储通信。这两个项目都基于春季启动,并实现为maven项目。

问题: 当我尝试启动dataintegration项目(运行Application.java),其中datahub项目通过maven依赖项集成时,我在线程结束时得到异常。项目datahub作为独立的演示应用程序工作。它也可以与弹簧引导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

2 个答案:

答案 0 :(得分:1)

您是否在运行的Application类中使用 @Configuration 尝试 @EnableJpaRepositories

我为我的一个类似于你的项目尝试了类似的东西,它对我有用;

@Configuration
@EnableJpaRepositories("com.example.mcp")
@EntityScan("com.example.mcp")

WorkingDay有 @Entity 注释。

答案 1 :(得分:0)

我已经解决了这个问题。以下帖子非常有用:Spring Boot And Multi-Module Maven Projects

我将文件com.example.mcp.dataintegration.Application.java移动到com.example.mcp.Application.java。但是还不清楚为什么我的ComponentScan和JPARepo定义被忽略了......