问题排查ContextLoader,项目启动时,日志报错

本文探讨了Spring框架中数据源配置出现的问题,特别是在启动时因未能正确解析配置文件中的占位符导致的异常。文章详细分析了错误日志,指出问题根源在于Spring未能从指定的.properties文件中读取到必要的配置信息,并提供了使用PropertyPlaceholderConfigurer来解决该问题的方法。

问题排查ContextLoader

项目启动时,日志报错:

ERROR org.springframework.web.context.ContextLoader - Context initialization failedorg.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'driverClassName' defined in null: Could not resolve placeholder 'driverClassName' in string value "${driverClassName}"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'elasticsearch_cluster_nodes' in string value "${elasticsearch_cluster_nodes}" at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:211) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.processProperties(PropertyPlaceholderConfigurer.java:222) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:86) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]

排查过程

1.根据日志提示找不到数据源字段的具体值,数据源的相关配置是放在.properties文件中的,但是Spring在加载相关bean的时候,没有找到这些值。

2.使用PropertyPlaceholderConfigurer将数据源配置文件加载进来。

<bean  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
   <property name="locations">
      <list>
         <value>file:#{systemProperties['db.properties']}</value>
      </list>
   </property>
   <property name="order" value="0" />
</bean>

 

springboot项目单元测试时,报java.lang.IllegalStateException: Failed to load ApplicationContext for [WebMergedContextConfiguration@727320fa testClass = com.example.lpro.mybatisPlus.api.SyUserControllerTest, locations = [], classes = [com.example.lpro.LProApplication], contextInitializerClasses = [], activeProfiles = [], propertySourceDescriptors = [], propertySourceProperties = ["org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true"], contextCustomizers = [[ImportsContextCustomizer@2935fd2c key = [@org.springframework.context.annotation.Import(value={org.mybatis.spring.annotation.MapperScannerRegistrar.class}), @org.mybatis.spring.annotation.MapperScan(annotationClass=java.lang.annotation.Annotation.class, basePackageClasses={}, basePackages={"com.example.lpro.mapper"}, defaultScope="", factoryBean=org.mybatis.spring.mapper.MapperFactoryBean.class, lazyInitialization="", markerInterface=java.lang.Class.class, nameGenerator=org.springframework.beans.factory.support.BeanNameGenerator.class, processPropertyPlaceHolders=true, sqlSessionFactoryRef="", sqlSessionTemplateRef="", value={"com.example.lpro.mapper"}), @org.springframework.boot.test.context.SpringBootTest(args={}, classes={}, properties={}, useMainMethod=NEVER, value={}, webEnvironment=MOCK), @org.apiguardian.api.API(consumers={"*"}, since="5.0", status=STABLE), @org.springframework.test.context.BootstrapWith(value=org.springframework.boot.test.context.SpringBootTestContextBootstrapper.class)]], org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@68c9133c, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@5b218417, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@6c284af, org.springframework.boot.test.web.reactor.netty.DisableReactorResourceFactoryGlobalResourcesContextCustomizerFactory$DisableReactorResourceFactoryGlobalResourcesContextCustomizerCustomizer@6be968ce, org.springframework.boot.test.autoconfigure.OnFailureConditionReportContextCustomizerFactory$OnFailureConditionReportContextCustomizer@1033576a, org.springframework.boot.test.autoconfigure.actuate.observability.ObservabilityContextCustomizerFactory$DisableObservabilityContextCustomizer@1f, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizer@6c4906d3, org.springframework.boot.test.context.SpringBootTestAnnotation@129c1300], resourceBasePath = "src/main/webapp", contextLoader = org.springframework.boot.test.context.SpringBootContextLoader, parent = null] 请帮我分析错误原因
06-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值