Spring 引导加载应用程序上下文失败
Spring boot Failed to load applicationcontext
有一个 springboot 应用程序,它使用 file-inbound-adapter.Written junit 测试用例从源目录读取文件。 Junit 测试用例在我的本地 eclipse 中成功执行。但在 运行 来自部署在另一台机器上的 bamboo/jenkins 时面临问题。
在执行 spring 引导 junit 测试用例时无法加载 applicationcontext。 'filesInChannel.adapter.source' 原因:java.lang.IllegalArgumentException: 源目录 [/filepath] 不存在。
这是什么原因造成的。关于这个有任何 suggestion/advice 吗?是由于 bamboo 部署机器的某些权限问题吗?
已经为测试用例编写了单独的配置。但不确定为什么它指向原始 xml 以在执行测试用例时加载应用程序上下文。
错误:
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'filesInChannel.adapter': Cannot resolve reference to bean 'filesInChannel.adapter.source' while setting bean property 'source'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'filesInChannel.adapter.source': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: Source directory [/filepath] does not exist.
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'filesInChannel.adapter.source': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: Source directory [/filepath] does not exist.
Caused by: java.lang.IllegalArgumentException: Source directory [/filepath] does not exist.
文件的源代码-process.xml :
<file:inbound-channel-adapter id="filesInChannel"
directory="/filepath/" auto-startup="false" scanner="recursiveScanner" auto-create-directory="true">
<integration:poller id="poller" max-messages-per-poll="${max.messages.per.poll}" fixed-rate="${message.read.frequency}" task-executor="pollingExecutor">
<integration:transactional transaction-manager="transactionManager" />
</integration:poller>
</file:inbound-channel-adapter>
测试用例:
@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = TestConfig.class)
public class FileApplicationTests {
@Test
public void contextLoads() {
}
}
测试配置:
@Configuration
@ImportResource({ "classpath:test-file-process.xml" })
public class TestConfig {
}
测试文件的源代码-process.xml :
<file:inbound-channel-adapter id="filesInChannel"
directory="file:${java.io.tmpdir}/filepath/" auto-startup="false" scanner="recursiveScanner" auto-create-directory="true">
<integration:poller id="poller" max-messages-per-poll="${max.messages.per.poll}" fixed-rate="${message.read.frequency}" task-executor="pollingExecutor">
<integration:transactional transaction-manager="transactionManager" />
</integration:poller>
</file:inbound-channel-adapter>
日志:
018-09-11 14:54:39.893 INFO 9224 --- [ main] c.o.i.f.h.ErrorHandlerTest : Starting ErrorHandlerTest on pc226116 with PID 9224 (started by test in D:\Repo\file-handler)
2018-09-11 14:54:39.896 INFO 9224 --- [ main] c.o.i.f.h.ErrorHandlerTest : No active profile set, falling back to default profiles: default
2018-09-11 14:54:39.901 INFO 9224 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@22dc59b2: startup date [Tue Sep 11 14:54:39 IST 2018]; root of context hierarchy
2018-09-11 14:54:39.910 INFO 9224 --- [ main] o.s.b.f.x.XmlBeanDefinitionReader : Loading XML bean definitions from class path resource [test-file-process.xml]
2018-09-11 14:54:40.158 INFO 9224 --- [ main] o.s.i.c.IntegrationRegistrar : No bean named 'integrationHeaderChannelRegistry' has been explicitly defined. Therefore, a default DefaultHeaderChannelRegistry will be created.
2018-09-11 14:54:40.161 INFO 9224 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'configurationValidator' with a different definition: replacing [Generic bean: class [com.interfaces.file.handler.ConfigurationValidator]; scope=singleton; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [D:\Repo\file-handler\target\classes\com\interfaces\file\handler\ConfigurationValidator.class]] with [Generic bean: class [com.interfaces.file.handler.ConfigurationValidator]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=init; destroyMethodName=null; defined in class path resource [test-file-process.xml]]
2018-09-11 14:54:40.164 INFO 9224 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'cacheManager' with a different definition: replacing [Generic bean: class [com.interfaces.file.handler.CacheManager]; scope=singleton; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [D:\Repo\file-handler\target\classes\com\interfaces\file\handler\CacheManager.class]] with [Generic bean: class [org.springframework.cache.support.SimpleCacheManager]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [test-file-process.xml]]
2018-09-11 14:54:40.167 INFO 9224 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'headerEnricher' with a different definition: replacing [Generic bean: class [com.interfaces.file.handler.HeaderEnricher]; scope=singleton; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [D:\Repo\file-handler\target\classes\com\interfaces\file\handler\HeaderEnricher.class]] with [Generic bean: class [com.interfaces.file.handler.HeaderEnricher]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [test-file-process.xml]]
2018-09-11 14:54:40.182 INFO 9224 --- [ main] .f.x.DefaultBeanDefinitionDocumentReader : Skipped XML bean definition file due to specified profiles [aws] not matching: class path resource [test-file-process.xml]
2018-09-11 14:54:40.548 INFO 9224 --- [ main] o.s.b.f.x.XmlBeanDefinitionReader : Loading XML bean definitions from class path resource [file-process.xml]
2018-09-11 14:54:40.795 INFO 9224 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'transactionManager' with a different definition: replacing [Generic bean: class [org.springframework.integration.transaction.PseudoTransactionManager]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [test-file-process.xml]] with [Generic bean: class [org.springframework.integration.transaction.PseudoTransactionManager]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [file-process.xml]]
我认为这里的问题 <file:inbound-channel-adapter>
实际上创建了几个 bean,其中之一是 FileReadingMessageSource
,条件如下:
Assert.isTrue(this.directory.exists(),
"Source directory [" + this.directory + "] does not exist.");
只是在其afterPropertiesSet()
独立于auto-startup="false"
。
恐怕正是这个 bean 没有被覆盖,只是因为它的 id 是自动生成的。
这就是另一个配置没有帮助的原因。
我建议您查看 配置文件 支持:https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-definition-profiles
然后将 <file:inbound-channel-adapter>
包装成如下内容:
<beans:beans profile="!test">
</beans:beans>
有一个 springboot 应用程序,它使用 file-inbound-adapter.Written junit 测试用例从源目录读取文件。 Junit 测试用例在我的本地 eclipse 中成功执行。但在 运行 来自部署在另一台机器上的 bamboo/jenkins 时面临问题。
在执行 spring 引导 junit 测试用例时无法加载 applicationcontext。 'filesInChannel.adapter.source' 原因:java.lang.IllegalArgumentException: 源目录 [/filepath] 不存在。
这是什么原因造成的。关于这个有任何 suggestion/advice 吗?是由于 bamboo 部署机器的某些权限问题吗?
已经为测试用例编写了单独的配置。但不确定为什么它指向原始 xml 以在执行测试用例时加载应用程序上下文。
错误:
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'filesInChannel.adapter': Cannot resolve reference to bean 'filesInChannel.adapter.source' while setting bean property 'source'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'filesInChannel.adapter.source': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: Source directory [/filepath] does not exist.
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'filesInChannel.adapter.source': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: Source directory [/filepath] does not exist.
Caused by: java.lang.IllegalArgumentException: Source directory [/filepath] does not exist.
文件的源代码-process.xml :
<file:inbound-channel-adapter id="filesInChannel"
directory="/filepath/" auto-startup="false" scanner="recursiveScanner" auto-create-directory="true">
<integration:poller id="poller" max-messages-per-poll="${max.messages.per.poll}" fixed-rate="${message.read.frequency}" task-executor="pollingExecutor">
<integration:transactional transaction-manager="transactionManager" />
</integration:poller>
</file:inbound-channel-adapter>
测试用例:
@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = TestConfig.class)
public class FileApplicationTests {
@Test
public void contextLoads() {
}
}
测试配置:
@Configuration
@ImportResource({ "classpath:test-file-process.xml" })
public class TestConfig {
}
测试文件的源代码-process.xml :
<file:inbound-channel-adapter id="filesInChannel"
directory="file:${java.io.tmpdir}/filepath/" auto-startup="false" scanner="recursiveScanner" auto-create-directory="true">
<integration:poller id="poller" max-messages-per-poll="${max.messages.per.poll}" fixed-rate="${message.read.frequency}" task-executor="pollingExecutor">
<integration:transactional transaction-manager="transactionManager" />
</integration:poller>
</file:inbound-channel-adapter>
日志:
018-09-11 14:54:39.893 INFO 9224 --- [ main] c.o.i.f.h.ErrorHandlerTest : Starting ErrorHandlerTest on pc226116 with PID 9224 (started by test in D:\Repo\file-handler)
2018-09-11 14:54:39.896 INFO 9224 --- [ main] c.o.i.f.h.ErrorHandlerTest : No active profile set, falling back to default profiles: default
2018-09-11 14:54:39.901 INFO 9224 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@22dc59b2: startup date [Tue Sep 11 14:54:39 IST 2018]; root of context hierarchy
2018-09-11 14:54:39.910 INFO 9224 --- [ main] o.s.b.f.x.XmlBeanDefinitionReader : Loading XML bean definitions from class path resource [test-file-process.xml]
2018-09-11 14:54:40.158 INFO 9224 --- [ main] o.s.i.c.IntegrationRegistrar : No bean named 'integrationHeaderChannelRegistry' has been explicitly defined. Therefore, a default DefaultHeaderChannelRegistry will be created.
2018-09-11 14:54:40.161 INFO 9224 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'configurationValidator' with a different definition: replacing [Generic bean: class [com.interfaces.file.handler.ConfigurationValidator]; scope=singleton; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [D:\Repo\file-handler\target\classes\com\interfaces\file\handler\ConfigurationValidator.class]] with [Generic bean: class [com.interfaces.file.handler.ConfigurationValidator]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=init; destroyMethodName=null; defined in class path resource [test-file-process.xml]]
2018-09-11 14:54:40.164 INFO 9224 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'cacheManager' with a different definition: replacing [Generic bean: class [com.interfaces.file.handler.CacheManager]; scope=singleton; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [D:\Repo\file-handler\target\classes\com\interfaces\file\handler\CacheManager.class]] with [Generic bean: class [org.springframework.cache.support.SimpleCacheManager]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [test-file-process.xml]]
2018-09-11 14:54:40.167 INFO 9224 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'headerEnricher' with a different definition: replacing [Generic bean: class [com.interfaces.file.handler.HeaderEnricher]; scope=singleton; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [D:\Repo\file-handler\target\classes\com\interfaces\file\handler\HeaderEnricher.class]] with [Generic bean: class [com.interfaces.file.handler.HeaderEnricher]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [test-file-process.xml]]
2018-09-11 14:54:40.182 INFO 9224 --- [ main] .f.x.DefaultBeanDefinitionDocumentReader : Skipped XML bean definition file due to specified profiles [aws] not matching: class path resource [test-file-process.xml]
2018-09-11 14:54:40.548 INFO 9224 --- [ main] o.s.b.f.x.XmlBeanDefinitionReader : Loading XML bean definitions from class path resource [file-process.xml]
2018-09-11 14:54:40.795 INFO 9224 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'transactionManager' with a different definition: replacing [Generic bean: class [org.springframework.integration.transaction.PseudoTransactionManager]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [test-file-process.xml]] with [Generic bean: class [org.springframework.integration.transaction.PseudoTransactionManager]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [file-process.xml]]
我认为这里的问题 <file:inbound-channel-adapter>
实际上创建了几个 bean,其中之一是 FileReadingMessageSource
,条件如下:
Assert.isTrue(this.directory.exists(),
"Source directory [" + this.directory + "] does not exist.");
只是在其afterPropertiesSet()
独立于auto-startup="false"
。
恐怕正是这个 bean 没有被覆盖,只是因为它的 id 是自动生成的。
这就是另一个配置没有帮助的原因。
我建议您查看 配置文件 支持:https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-definition-profiles
然后将 <file:inbound-channel-adapter>
包装成如下内容:
<beans:beans profile="!test">
</beans:beans>