简体   繁体   English

Spring启动jar没有读取/解析application.properties根文件夹

[英]Spring boot jar not reading/parsing application.properties root folder

Situation 情况

I have a fat .jar of a Spring boot application. 我有一个胖的.jar的Spring启动应用程序。 I've externalized my configuration with an application.properties file. 我使用application.properties文件外部化了我的配置。 This file is in the same folder as the .jar, and I'm starting the .jar from the command line from within the same folder (with the command "java -jar $jarFileName"). 此文件与.jar位于同一文件夹中,我从同一文件夹中的命令行启动.jar(使用命令“java -jar $ jarFileName”)。

Then an exception is thrown: 然后抛出异常:

nested exception is org.springframework.beans.TypeMismatchException: 
Failed to convert value of type 'java.lang.String' to required type 'int'; nested exception is 
java.lang.NumberFormatException: For input string: "${elasticsearch.port}"

As you can see, instead of reading the value from the properties file, it just sets the string as the text in the @Value annotation, which looks like this: 如您所见,它不是从属性文件中读取值,而是将字符串设置为@Value注释中的文本,如下所示:

@Value("${elasticsearch.port}")
private int elkPort;

The class this happens in is annotated with @Component . 发生在这里的类用@Component注释。 According to Spring docs: externalized configuration , spring should read an application.properties file outside of the jar. 根据Spring docs:外化配置 ,spring应该读取jar外的application.properties文件。

When the same application.properties file is placed in src/main/resources it works fine, so the configuration file seems correct. 当相同的application.properties文件放在src/main/resources它工作正常,因此配置文件似乎是正确的。

Any ideas why it won't load the external configuration file? 有什么想法它不会加载外部配置文件?

EDIT 1 I've also tried running it with --spring.config.location=file:application.properties and --spring.config.location=file:/full/path/to/application.properties but with the same result as above. 编辑1我也尝试使用--spring.config.location=file:application.properties--spring.config.location=file:/full/path/to/application.properties运行它,但结果与以上。

EDIT 2: classpath attempt Also tried classpath instead of file , the same as the commands above but file replaced with classpath . 编辑2:类路径尝试也尝试了classpath而不是file ,与上面的命令相同,但file替换为classpath Lastly tried without either, so just --spring.config.location=/path/to/file ; 最后尝试没有,所以只需--spring.config.location=/path/to/file ; again both with relative and full path to the application.properties . 再次使用application.properties相对路径和完整路径。 All attempts gave the same result/exception. 所有尝试都给出了相同的结果/异常。

EDIT 3 My annotated application: 编辑3我的注释应用程序:

@SpringBootApplication
public class ApplicationName {

    public static void main(String[] args) {
        SpringApplication.run(ApplicationName.class, args);
    }
}

EDIT 4 Tried adding a PropertySourcesPlaceholderConfigurer as follows: 编辑4尝试添加PropertySourcesPlaceholderConfigurer ,如下所示:

@Configuration
public class PropertyConfig {

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}

And then for each @Value I added a default value; 然后为每个@Value我添加了一个默认值; it still only resolves to the default values instead of to the application.properties values. 它仍然只能解析为默认值而不是application.properties值。

As it says on mentioned page, you should specify external config location 如上所述,您应该指定外部配置位置

    $ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties

Try without file keyword --spring.config.location=/full/path/application.properties 尝试不使用文件关键字--spring.config.location = / full / path / application.properties

I just took my application.properties out of an Eclipse Spring Boot project and it failed. 我刚从Eclipse Spring Boot项目中取出了我的application.properties,但它失败了。 Then I put the file in a cfg folder in the root of the project and added program argument: 然后我将文件放在项目根目录下的cfg文件夹中,并添加了程序参数:

--spring.config.location=cfg/application.properties

and it worked again. 它再次起作用。 Mayby if you try a relative path (no leading /) to the file (without the "file:") it will work. Mayby如果你尝试一个相对路径(没有前导/)到文件(没有“file:”)它将工作。

Alright after quite some struggles, I've found the solution. 好吧经过一番挣扎,我找到了解决方案。 I was close with PropertySourcesPlaceholderConfigurer but not quite there yet; 我与PropertySourcesPlaceholderConfigurer关系密切,但还没到那里; this is the full class now: 现在这是全班:

@Configuration
public class PropertyConfig {

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        final PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();

        ppc.setIgnoreResourceNotFound(true);

        final List<Resource> resources = new ArrayList<>();

        resources.add(new FileSystemResource("relative/path/to/application.properties"));

        ppc.setLocations(resources.toArray(new Resource[]{}));

        return ppc;
    }
}

EDIT 编辑

To demonstrate the issue, I've created a repository to show the problem, see here: https://github.com/Locitao/test-external-properties 为了演示这个问题,我创建了一个存储库来显示问题,请参见此处: https//github.com/Locitao/test-external-properties

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring boot application.properties 文件读取 - Spring boot application.properties file reading spring boot jar中的application.properties文件在哪里? - Where are the application.properties files in a spring boot jar? Spring Boot - 从依赖的 jar 加载 application.properties/yml - Spring Boot - Load application.properties/yml from dependent jar 在jar spring boot外部目录中搜索application.properties - Search application.properties in directory external to jar spring boot 使用 gradle 在 spring boot jar 中排除 application.properties - Exclude application.properties inside spring boot jar using gradle Spring 引导应用程序。导出到 jar 后找不到属性 - Spring Boot application.properties not found after exporting to jar 为什么 Spring 引导不从默认的 application.properties 文件中读取 - Why is Spring Boot not reading from default application.properties file 从 application.properties Spring Boot 中读取值 - Reading values from application.properties Spring Boot 如何将Spring Boot application.properties外部化到tomcat / lib文件夹 - How to externalize Spring Boot application.properties to tomcat/lib folder 用spring.config.location运行Spring boot .jar文件,并替换application.properties的值? - Run Spring boot .jar file with spring.config.location with replace values of application.properties?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM