1.前言
在使用Springboot时,我们偶尔会单独引用一些特定的properties文件,在引用这些文件时,我们就应用到了注解:@PropertySource
。
2.分析错误信息
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.zxycloud.common.MybatisGeneratorApplication]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/redisConfig.properties]
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:181)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:315)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:232)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:275)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:95)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:691)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:528)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:127)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:117)
... 25 common frames omitted
错误的核心信息是找不到一个properties文件,起初我还以为是maven中的build没有配置好resources,但是检查后发现并不是。仔细看一眼错误,才开始怀疑是否是代码中写错了,果不其然。代码中写配置文件路径的时候没有加上classpath。 正常的使用的代码应该像该类给出的示例那样:
@PropertySource("classpath:/com/myco/a.properties")
当然,如果就在resources
下,直接就:
@PropertySource("classpath:a.properties")
3.总结
使用注解时,应该查看一下源代码的注释中是否有详细的解释,显然作者们给出了大量的例子。