目次
事象
Spring batch Eclipse上では動作するがjarからの起動でエラー(UnsatisfiedDependencyException)
Eclipseで実行とかデバッグとかでは正常に動作していたが、maven buildして生成したjarを実行したところ異常終了となった。
エラー内容
C:\opt\java>java -cp "app\*;lib\*" org.springframework.batch.core.launch.support.CommandLineJobRunner META-INF/jobs/Hoge/Hoge.xml Hoge puge=pupuge
[2018/10/30 16:43:17] [main] [o.s.c.s.ClassPathXmlApplicationContext] [INFO ] Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@757c7103: startup date [Tue Oct 30 16:43:17 JST 2018]; root of context hierarchy
[2018/10/30 16:43:17] [main] [o.s.b.f.x.XmlBeanDefinitionReader] [INFO ] Loading XML bean definitions from class path resource [META-INF/jobs/Hoge/Hoge.xml]
[2018/10/30 16:43:17] [main] [o.s.b.f.x.XmlBeanDefinitionReader] [INFO ] Loading XML bean definitions from class path resource [META-INF/spring/job-base-context.xml]
[2018/10/30 16:43:17] [main] [o.s.b.f.x.XmlBeanDefinitionReader] [INFO ] Loading XML bean definitions from class path resource [META-INF/spring/launch-context.xml]
[2018/10/30 16:43:18] [main] [o.s.b.f.c.PropertyPlaceholderConfigurer] [INFO ] Loading properties file from class path resource [batch-application.properties]
[2018/10/30 16:43:18] [main] [o.s.b.f.c.PropertyPlaceholderConfigurer] [INFO ] Loading properties file from class path resource [ValidationMessages.properties]
[2018/10/30 16:43:18] [main] [o.s.b.f.s.DefaultListableBeanFactory] [INFO ] Overriding bean definition for bean 'HogeTasklet' with a different definition: replacing [Generic bean: class [itouoti.Hoge.HogeTasklet]; scope=step; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=false; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in URL [jar:file:/C:/tmp/java/app/sample-1.0.0-SNAPSHOT.jar!/itouoti/Hoge/HogeTasklet.class]] with [Root bean: class [org.springframework.aop.scope.ScopedProxyFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true
; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in BeanDefinition defined in URL [jar:file:/C:/tmp/java/app/sample-1.0.0-SNAPSHOT.jar!/itouoti/Hoge/HogeTasklet.class]]
[2018/10/30 16:43:18] [main] [o.s.b.f.a.AutowiredAnnotationBeanPostProcessor] [INFO ] JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
[2018/10/30 16:43:18] [main] [o.s.c.s.PostProcessorRegistrationDelegate$BeanPostProcessorChecker] [INFO ] Bean 'jobRegistry' of type [class org.springframework.batch.core.configuration.support.MapJobRegistry] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
[2018/10/30 16:43:18] [main] [o.s.b.c.r.s.JobRepositoryFactoryBean] [INFO ] No database type set, using meta data indicating: DB2
[2018/10/30 16:43:19] [main] [o.s.b.c.l.s.SimpleJobLauncher] [INFO ] No TaskExecutor has been set, defaulting to synchronous executor.
[2018/10/30 16:43:19] [main] [o.h.v.i.util.Version ] [INFO ] HV000001: Hibernate Validator 5.2.4.Final
[2018/10/30 16:43:19] [main] [o.s.c.s.ClassPathXmlApplicationContext] [WARN ] Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'HogeServiceImpl': Unsatisfied dependency expressed through field 'sharedService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'itouoti.sharedService.SharedService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.inject.Inject()}
[2018/10/30 16:43:19] [main] [o.s.b.c.l.s.CommandLineJobRunner] [ERROR] Job Terminated in error: Error creating bean with name 'HogeServiceImpl': Unsatisfied dependency expressed through field 'sharedService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'itouoti.sharedService.SharedService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.inject.Inject()}
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'HogeServiceImpl': Unsatisfied dependency expressed through field 'sharedService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'itouoti.sharedService.SharedService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.inject.Inject()}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1225)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:552)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:759)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at org.springframework.batch.core.launch.support.CommandLineJobRunner.start(CommandLineJobRunner.java:290)
at org.springframework.batch.core.launch.support.CommandLineJobRunner.main(CommandLineJobRunner.java:592)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'itouoti.sharedService.SharedService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.inject.Inject()}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1474)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1102)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1064)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
... 16 common frames omitted
原因
すごくしょうもないけどかなり時間がかかってしまった。
結果として、component-scanで読み込んでいたpackageが小文字となっていた。
eclipseでは小文字大文字を判定しない?
原因となったxmlのcomponent-scanタグ
<context:component-scan
base-package="itouoti.batch.Hoge,
itouoti.Hoge,
itouoti.sharedservice,
itouoti.repository.Hoge,
itouoti.repository.sharedRepository,
itouoti.repository.commonDTO" />
「itouoti.sharedService」が正しい
解決策
パッケージの小文字になっているところを大文字にする。
<context:component-scan
base-package="itouoti.batch.Hoge,
itouoti.Hoge,
itouoti.sharedService,
itouoti.repository.Hoge,
itouoti.repository.sharedRepository,
itouoti.repository.commonDTO" />