简体   繁体   中英

Spring adds application.properties with valiues from external properties to classpath, and how to disable it?

The question(?)

I've made the application properties external because of the database configuration and API keys but through some spring magic they get read and only spring valiues are left and placed into the classpath, overriding the external properties later on deployment. So how would I disable this "feature", so that it wont include the properties inside the jar?

The code I used to make the properties external

@Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        PropertySourcesPlaceholderConfigurer properties =
                new PropertySourcesPlaceholderConfigurer();
        properties.setLocation(new FileSystemResource("program.properties"));
        properties.setIgnoreResourceNotFound(false);
        return properties;
    }

I have a suspition that the placeholder part of PropertySourcesPlaceholderConfigurer has the difrence, but my nonnative English research skills couldn't find any results.

Edits

  • It is packaged as a jar with bundled tomcat
  • Properties file is located in the home dir of the jar

Ive found the solution.

Introduction

Spring compiles the properties inside the file if you use the code snippet most commonly provided with the question "How to make application.properties external?" . Its probably intentional misleading but lets not get contraversial.

The solution

First edit the code that you use to configure spring as this is the right solution (atleast for me)

        SpringApplication application = new SpringApplication(Application.class);

        Properties properties = new Properties();
        try {
            properties.load(new FileReader("application.properties"));
            application.setDefaultProperties(properties);
            application.run(args);
        } catch (IOException e) {
            e.printStackTrace();
        }

Second you need to edit the maven configuration to not include the file

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <addResources>false</addResources>
                </configuration>
            </plugin>

And if that doesnt work check the target directory if application.properties is still left there.

Hopefully this will be useful for people that had the same headache as me. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM