2016-06-25 5 views
0

Springブートで「バッチ」を開発したいと思います。私はCommandLineRunnerを実装しているクラスがあり、そのクラスには@Serviceクラスが必要です。ここでCommandLineRunnerの依存性注入が失敗しました

はコードです:

@SpringBootApplication 
public class CarmindBatch implements CommandLineRunner { 

    @Inject 
    private VehicleDataBatchService vehicleDataBatchService; 

    @Override 
    public void run(String... args) { 

     vehicleDataBatchService.start(args[0]); 
    } 

    public static void main(String[] args) { 

     SpringApplication.run(CarmindBatch.class, args); 
    } 
} 

とサービス:

@Service 
public class DefaultVehicleDataBatchService implements VehicleDataBatchService { 

    @Inject 
    private VehicleDataRepository vehicleDataRepository; 

    @Inject 
    private Mapper mapper; 

    @Override 
    public void start(String path) { 

     // Convert a file to a list of beans and put them in database... 
    } 

} 

私の問題は、私はCarmindBatchを起動したときBeanCreationExceptionが発生したが、私は誰のJUnitテストをしようとすると、それがうまく機能していることです注入VehicleDataBatchService

スタックトレース:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'carmindBatch': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private fr.carmind.api.service.batch.VehicleDataBatchService fr.carmind.api.batch.CarmindBatch.vehicleDataBatchService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [fr.carmind.api.service.batch.VehicleDataBatchService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.inject.Inject()} 
    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.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE] 
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE] 
    at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE] 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE] 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE] 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE] 
    at fr.carmind.api.batch.CarmindBatch.main(CarmindBatch.java:25) [classes/:na] 
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private fr.carmind.api.service.batch.VehicleDataBatchService fr.carmind.api.batch.CarmindBatch.vehicleDataBatchService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [fr.carmind.api.service.batch.VehicleDataBatchService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.inject.Inject()} 
    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] 
    ... 17 common frames omitted 
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [fr.carmind.api.service.batch.VehicleDataBatchService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.inject.Inject()} 
    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.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) ~[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] 
    ... 19 common frames omitted 

は、あなたが任意のアイデアを持っていますか?

+0

DefaultVehicleDataBatchServiceのパッケージとは何ですか? –

+0

@JBNizet:fr.carmind.api.service.batch.impl – BnJ

答えて

1

お客様のアプリケーションクラスは、パッケージfr.carmind.api.batchにあります。

サービスクラスは、fr.carmind.api.service.batch.implです。

スキャンするパッケージを指定していないため、デフォルトではアプリケーションクラスパッケージとすべてのサブパッケージをスキャンします。しかし、サービスクラスはサブパッケージに含まれていません。それは見つからない。

サービスクラスをアプリケーションクラスのサブパッケージに配置するか、@ComponentScan注釈を使用してスキャンするパッケージを指定します。

+0

ありがとうございます。私はそれを見ていないために少し愚かな感じ:P ... – BnJ

関連する問題