简体   繁体   中英

Use properties file from tomcat folder, instead of classpath

I created a Spring Boot project which contains a properties file called wmdl_app.properties and is located in src/main/config with some configurations and some credentials, which need to be changed depending on the environment the project runs on. I also have this file in tomcat folder->instance-config

Seems like the config I made for this project is wrong, because instead of reading the file with properties from the instance-config folder in Tomcat, it reads the file from src/main/config.

What changes should I make in order to make the app look into the instance-config folder for this file, instead of the classpath?

My pom contains this reference to the resources, which if deleted, causes my project to fail because it cannot find the properties file and also the log4j config - they are both placed in src/main/config

<resources>
            <resource>
                <directory>${project.basedir}/src/main/resources</directory>
            </resource>
            <resource>
                <directory>${project.basedir}/src/main/config</directory>
            </resource>
        </resources>

The class which contains the reference to the properties file, I don't have it anywhere else in the whole app:

@SpringBootApplication( scanBasePackages = {"com.db.wmdl.glue2g.*","com.db.wmdl.fo.service","com.db.wmdl.fo.persistence", "com.db.wmdl.fo.serversync"})
@PropertySource("classpath:wmdl_app.properties")
@EnableScheduling
public class Application {

    @PostConstruct
    public void init() throws IOException {
        LoggerContext context = (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false);
        context.setConfigLocation(new ClassPathResource("log4j-dl-fo-config.xml").getURI());
    }

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

}

Please tell me what should I change in order to make it get the properties from the file which is in instance-config from Tomcat.

This article mentions the possibility of passing in a spring.config.location variable that can be used to point where Spring Boot should look for resources. I'm not sure you can have both though (properties in the JAR plus properties outside).

As per the code

@PropertySource("classpath:wmdl_app.properties")

the property file is read from classpath. So whatever is inside of src/main/resources would have gone under the classpath inside the JAR.

@PropertySource(value = {"classpath:wmdl_app.properties", "file:/absolute/path/to/file"})

can be used to directly read a property file from a file location.

Since you mentioned it is environment specific where project runs, you can instead us Spring profiles

Thus you can have wmdl_app_dev.properties & wmdl_app_qa.properties

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