【发布时间】:2023-03-13 09:03:01
【问题描述】:
我有一个简单的 gradle spring boot java 应用程序,我正在尝试使用 spring boot 分析从“application.properties”和“application-dev.properties”获取一些属性值。它工作正常,当我尝试在本地机器上运行应用程序时,spring boot 配置文件正在加载,但是当我尝试在 docker 上运行相同的应用程序时,突然弹出错误消息,提示应用程序无法在类路径。
下面是项目结构
在 App.Config 类中,我有以下代码。如您所见,我正在尝试从 application.properties 文件中获取属性值。
@Component
@PropertySource("classpath:application.properties")
public class AppConfig {
@Value("${host}")
private String host;
@Value("${map}")
private String map;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getMap() {
return map;
}
public void setMap(String map) {
this.map = map;
}
}
application.properties 包含以下代码。
map = Main-Map
spring.profiles.active=${profile}
application-dev.properties 包含以下代码
host = Development-host
如您所见,我正在从外部在 application.properties 中设置配置文件值。这就是我试图通过 docker 注入的内容
Dockerfile 包含以下代码
FROM java:8
VOLUME /tmp
ENV tom=dev
ADD build/libs/demo-0.0.1-SNAPSHOT.jar /app/app.jar
ADD build/resources/main/application.properties /app/application.properties
ADD build/resources/main/application-dev.properties /app/application-dev.properties
WORKDIR /app
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-
Dprofile=${tom}","-jar","app.jar", "-
-spring.config.location=/app/application.properties, /app/application-
dev.properties"]
我使用以下命令构建 docker 映像
docker build -t demo:latest .
我使用以下命令运行 docker
docker run -p 8083:8080 demo:latest
当我运行 docker run 命令时,出现异常。
2019-12-12 07:09:50.405 INFO 1 --- [ main] com.example.demo.DemoApplication :
Starting DemoApplication on ed7cb11b8a34 with PID 1 (/app/app.jar started by root in /app)
2019-12-12 07:09:50.407 INFO 1 --- [ main] com.example.demo.DemoApplication : The
following profiles are active: dev
2019-12-12 07:09:50.603 WARN 1 --- [ main] ConfigServletWebServerApplicationContext :
Exception encountered during context initialization - cancelling refresh attempt:
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class
[com.example.demo.DemoApplication]; nested exception is java.io.FileNotFoundException: class path
resource [application.properties] cannot be opened because it does not exist
2019-12-12 07:09:50.712 ERROR 1 --- [ main] o.s.boot.SpringApplication :
Application run failed
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [ com.example.demo.DemoApplication]; nested exception is java.io.FileNotFoundException: class path
resource [ application.properties] cannot be opened because it does not exist
at
任何帮助将不胜感激。
提前致谢。
【问题讨论】:
-
关注我的 github 存储库并使用 docker 文件。它应该可以解决您的问题。 github.com/naveenkulkarni029/products-api
标签: java spring spring-boot dockerfile