2016-07-08 17 views
1

2番目のスプリングブートプロジェクトと組み合わせてautowiredに問題があります。うまくいけば、問題を解決するためのヒントを教えてください。BeanCreationException:別のMavenスプリングプロジェクトでautowired依存関係が失敗した

プロジェクト設定: プロジェクトdatahubは、データストアへのアクセスを管理します。プロジェクトのデータ蓄積は他にもいくつかありますが、データストアとの通信にプロジェクトデータハブが必要です。両方のプロジェクトはスプリングブートに基づいており、Mavenプロジェクトとして実現されています。

問題:私はDataHubのプロジェクトは、Mavenの依存関係を経由して統合されてdataintegrationプロジェクトを(Application.javaを実行)、起動しようとすると 、私は、スレッドの終了時に例外を取得します。プロジェクトのデータハブは、スタンドアローンのデモアプリケーションとして機能します。これはまた、スプリングブートWebアプリケーション(HTTPリクエストを介してデータストアエンティティを読み込む)と一緒に(依存関係としてpom.xmlを介して統合されて)一緒に機能します。

最も不満なポイントは、プロジェクトのデータ化がWebアプリケーションとして実現されているときに、同じ設定が機能していることです。しかし、ネイティブjavaアプリケーション(jar)としてdataintegrationが実現されているときは動作しません。

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 
+1

データ統合には、依存関係として 'spring-boot-starter-data-jpa'がありません。またサービスクラスの '@ ComponentScan'を削除してください。それは' @Configuration'クラスでのみ使用できるだけで無駄です。 –

+0

@ m-deinum:残念ながら、私は2番目のpom.xmlを忘れていました。既に添付されています。 spring-boot-starter-data-jpaは依存関係として利用できます。 –

答えて

0

私は既にこの問題を解決しました。次の投稿は非常に役に立ちました。Spring Boot And Multi-Module Maven Projects

ファイルcom.example.mcp.dataintegration.Application.javaをcom.example.mcp.Application.javaに移動しました。しかし、なぜ私のComponentScan JPARepo定義が無視されたのかはっきりしない...

0

あなたが実行したアプリケーションクラスの@Configuration@EnableJpaRepositoriesを試してみました。

私はあなたのプロジェクトに似ていて、私にとってはうまくいっています。

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

WorkingDayは@Entity注釈を持っています。

+0

私は** @ Configuration **と** @ EnableJpaRepositories **をApplication.classに追加しました。前と同じようないくつかのエラー.... –

関連する問題