简体   繁体   English

为什么是 ! 在docker上的spring-boot应用程序的属性文件路径中?

[英]Why is ! in the properties file path in spring-boot application on the docker?

I developed the rest API. 我开发了其余的API。 So, I tried to run it on the docker. 因此,我尝试在docker上运行它。 However, when I run it, it gives me file not found error. 但是,当我运行它时,它给了我文件找不到错误。

This is file list in the app.jar. 这是app.jar中的文件列表。 在此处输入图片说明

This is error message. 这是错误消息。

Caused by: java.io.FileNotFoundException: file:app.jar!/BOOT-INF/classes!/application_variable.properties (No such file or directory)
        at java.io.FileInputStream.open0(Native Method) ~[na:1.8.0_111]
        at java.io.FileInputStream.open(FileInputStream.java:195) ~[na:1.8.0_111]
        at java.io.FileInputStream.<init>(FileInputStream.java:138) ~[na:1.8.0_111]
        at java.io.FileInputStream.<init>(FileInputStream.java:93) ~[na:1.8.0_111]
        at com.t3q.userManage.utils.SSOProperties.<init>(SSOProperties.java:31) ~[classes!/:0.0.1-SNAPSHOT]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_111]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_111]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_111]
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_111]
        at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:172) ~[spring-beans-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
        ... 53 common frames omitted

How can I solve this problem? 我怎么解决这个问题? ps. PS。 It works well in the eclipse! 在日食中效果很好!

ADDED. 添加。 This is my Dockerfile. 这是我的Dockerfile。

FROM java:8
VOLUME /tmp
EXPOSE 8080
ADD target/userManageWithRest-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

This is SSOProperties. 这是SSOProperties。

public class SSOProperties {
    private static String servicePayloadDefault = "ID";
    private static final Logger logger = LoggerFactory.getLogger(SSOProperties.class);

    private String rootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
    private String propertiesPath = "application_variable.properties";
    private Properties props;


    public SSOProperties() throws FileNotFoundException, IOException {
        props = new Properties();
        rootPath = rootPath.replaceAll("%20", " ");
        rootPath = rootPath.replaceFirst("/", "");
        props.load(new FileInputStream(rootPath + propertiesPath));

        logger.info("rootPath="+rootPath);
    }
    ...
}

Caused by: java.io.FileNotFoundException: file:app.jar!/BOOT-INF/classes!/application_variable.properties (No such file or directory) 引起原因:java.io.FileNotFoundException:file:app.jar!/ BOOT-INF / classes!/application_variable.properties(无此类文件或目录)

That is standard way of Java to convey classloading of resources from archives. 这是Java传达档案中资源类加载的标准方法。
What is after the ! 之后是什么! is inside the app.jar file. app.jar文件中。
Same thing for classes! classes! but that is inside a directory in an archive (the jar). 但这位于归档文件(jar)中的目录内。

Your problem is there : 您的问题在那里:

private String rootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
//...
props.load(new FileInputStream(rootPath + propertiesPath));

You pass rootPath that is String path but that is included in a archive (the jar ), the FileInputStream cannot access to it. 您传递的rootPathString路径,但包含在存档( jar )中, FileInputStream无法访问它。
What you need is getting the inputstream from the archive and to pass it to the Properties.load() method: 您需要从档案中获取输入流,并将其传递给Properties.load()方法:

InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(propertiesPath);
props.load(is);

Note that as you use Spring you could also just do : 请注意,当您使用Spring时,您也可以这样做:

ClassPathResource resource = new ClassPathResource(propertiesPath);
props.load(resource.getInputStream());

And you could even automate the properties loading thanks to @PropertySource and @ConfigurationProperties : 通过@PropertySource@ConfigurationProperties甚至可以自动加载属性:

@Configuration
@PropertySource("classpath:application_variable.properties")
@ConfigurationProperties
public class VariableProperties {

    private String foo;
    private String bar;

    // getters and setters
}

And inject it wherever you need it. 并在您需要的任何地方注入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring-boot-每个级别的日志文件的应用程序属性配置 - Spring-boot - application properties configuration for log file per level Tomcat不读取Spring-Boot应用程序属性 - Tomcat Not reading Spring-Boot Application Properties 在docker容器中重新部署spring-boot应用程序? - Redeploy spring-boot application in docker container? 如何从 spring-boot 应用程序中的 application.yml 文件中读取属性 - How to read properties from application.yml file in spring-boot application Spring-boot应用程序无法在docker内启动时启动 - Spring-boot application won't boot at startup inside docker 如何在spring-boot 1.4.3中覆盖spring-boot应用程序属性 - How to override spring-boot application properties in spring-boot 1.4.3 有没有办法在 application.properties 文件中写入 JSON 数组列表(spring-boot) - Is there any way to write JSON array list in application.properties file (spring-boot) 如何从其他文件加载基于Spring-boot的REST应用程序属性? - how to load spring-boot based REST application properties from a different file? 如何在Spring Boot应用程序中从YML文件加载多个属性 - How to load multiple properties from YML file in spring-boot application 在 docker compose 文件中访问 Spring Boot 应用程序属性 - Access Spring boot application properties in docker compose file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM