如何避免 Spring Boot 应用程序中的“类路径资源 [...] 无法打开,因为它不存在”?

J. *_*Doe 2 classpath spring-boot

我有一个小的 Spring Boot 应用程序,它在 src/main/resources 中提供了一个 application.properties - 这就像一个魅力。用户现在应该能够配置更多内容(2 个凭据),因此我期望类路径中有一个名为“matrixregistration.config”的文件。不幸的是,无论在哪里都找不到这个文件。该应用程序通过 gradle spring boot 任务 bootDistZip 进行分发。

@Configuration
@PropertySource("classpath:matrixregistration.config")
@Validated
public class MatrixRegistrationConfiguration {

    private String sharedSecret;
    private String registrationPassword;
...
Run Code Online (Sandbox Code Playgroud)

我的分发包位于服务器上的 /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
Run Code Online (Sandbox Code Playgroud)

有人可以告诉我如何配置该任务或者我应该如何做才能使其正常工作吗?谢谢!

编辑:使用 @PropertySource("file:./matrixregistration.properties") 它可以工作。用户可以配置在“.”中查找的应用程序。用于矩阵注册.properties

Ahm*_*ylu 5

我遇到了同样的问题并通过“干净构建”解决了它。