Search code examples
javaspringspring-bootannotationsmicroservices

@PropertySource with the right classpath


I have a microservice (name = "microservice-porting.jar") inside a directory "/opt/microservice-porting/". Inside the directory, I have a config folder with an application properties file application.yml.

The structure is:

microservice-porting/
    microservice-porting.jar
    config/
        application.yaml

In my code:

@PropertySource(value = "classpath:/microservice-porting/config/application.yml", factory = MultipleYamlPropertySourceFactory.class)

but i always get exception:

Caused by: java.io.FileNotFoundException: class path resource [microservice-porting/config/application.yml] cannot be opened because it does not exist

All these files and directories are at a remote server where i deploy the jar files.

What is the right classpath for may case?


Solution

  • To access the application.yml file located in the config folder, specify the classpath as follows:

    @PropertySource(value = "classpath:/config/application.yml", factory = MultipleYamlPropertySourceFactory.class)
    

    Make sure the config folder is located in the same directory as your JAR file.