我有一个小的spring Boot 应用程序,它application.properties在src/main/resources中提供了一个www.example.com-它工作起来很有魅力。用户现在应该能够配置更多的东西(2个凭据),所以我希望在classpath中有一个名为“matrixregistration.config”的文件。不幸的是,无论在哪里都找不到这个文件。该应用程序与gradle spring boot任务bootDistZip一起分发。
@Configuration
@PropertySource("classpath:matrixregistration.config")
@Validated
public class MatrixRegistrationConfiguration {
private String sharedSecret;
private String registrationPassword;
...
我的分发包位于服务器上的/home/matrix/matrixregistration-boot,有一个“bin”和一个“lib”文件夹。我希望当我将配置放入这个目录并使用./bin/matrixregistration启动应用程序时,类路径应该包含“.”并找到配置。但它没有。配置文件在那里。
例外情况:
matrix@myhost:~/matrixregistration-boot$ ./bin/matrixregistration
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.4.RELEASE)
2019-04-21 10:04:47.332 INFO 25962 --- [ main] matrixregistration.Application : Starting Application on myhost.de with PID 25962 (/home/matrix/matrixregistration-boot/lib/matrixregistration.jar started by matrix in /home/matrix/matrixregistration-boot)
2019-04-21 10:04:47.342 INFO 25962 --- [ main] matrixregistration.Application : No active profile set, falling back to default profiles: default
2019-04-21 10:04:47.804 WARN 25962 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [matrixregistration.Application]; nested exception is java.io.FileNotFoundException: class path resource [matrixregistration.config] cannot be opened because it does not exist
2019-04-21 10:04:48.237 ERROR 25962 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [matrixregistration.Application]; nested exception is java.io.FileNotFoundException: class path resource [matrixregistration.config] cannot be opened because it does not exist
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:181) ~[spring-context-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:315) ~[spring-context-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:232) ~[spring-context-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:275) ~[spring-context-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:95) ~[spring-context-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:705) ~[spring-context-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:531) ~[spring-context-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.1.4.RELEASE.jar!/:2.1.4.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) ~[spring-boot-2.1.4.RELEASE.jar!/:2.1.4.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.1.4.RELEASE.jar!/:2.1.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) ~[spring-boot-2.1.4.RELEASE.jar!/:2.1.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) ~[spring-boot-2.1.4.RELEASE.jar!/:2.1.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) ~[spring-boot-2.1.4.RELEASE.jar!/:2.1.4.RELEASE]
at matrixregistration.Application.main(Application.java:10) ~[classes!/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) ~[matrixregistration.jar:na]
at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) ~[matrixregistration.jar:na]
at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) ~[matrixregistration.jar:na]
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) ~[matrixregistration.jar:na]
Caused by: java.io.FileNotFoundException: class path resource [matrixregistration.config] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:180) ~[spring-core-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
at org.springframework.core.io.support.EncodedResource.getInputStream(EncodedResource.java:159) ~[spring-core-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:99) ~[spring-core-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:73) ~[spring-core-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
at org.springframework.core.io.support.PropertiesLoaderUtils.loadProperties(PropertiesLoaderUtils.java:59) ~[spring-core-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
at org.springframework.core.io.support.ResourcePropertySource.<init>(ResourcePropertySource.java:67) ~[spring-core-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
at org.springframework.core.io.support.DefaultPropertySourceFactory.createPropertySource(DefaultPropertySourceFactory.java:37) ~[spring-core-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.processPropertySource(ConfigurationClassParser.java:452) ~[spring-context-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:271) ~[spring-context-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:242) ~[spring-context-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:191) ~[spring-context-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:295) ~[spring-context-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:242) ~[spring-context-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:199) ~[spring-context-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:167) ~[spring-context-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
... 21 common frames omitted
有人能告诉我如何配置该任务或我应该如何做才能让它工作?谢谢!
edit:with @PropertySource(“file:./matrixregistration.properties“)它可以工作。用户可以配置应用程序,在“.”中查找matrixregistration.properties
3条答案
按热度按时间4xrmg8kj1#
我也遇到了同样的问题,并通过“清洁构建”解决了它。
qeeaahzv2#
我也遇到过类似的问题,我认为你必须把绝对路径放在@PropertySource(“classpath:matrixregistration.config”)中。
例如:
@PropertySource(“classpath:path/from/src/matrixregistration.config”)
我希望它能帮助你。
fdbelqdn3#
当我的文件名为
_
时,我也遇到了同样的错误。删除文件名中的下划线为我修复。我的文件名是
my_file_name.csv
,将其更改为my-file-name.csv
可以工作。