@PropertySource使用正确的类路径

huangapple go评论52阅读模式
英文:

@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 an 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 on a remote server where I deploy the jar files.

What is the right classpath for my case?

英文:

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?

答案1

得分: 1

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.

英文:

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.

huangapple
  • 本文由 发表于 2023年5月22日 18:40:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76305349.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定