繁体   English   中英

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

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

我开发了其余的API。 因此,我尝试在docker上运行它。 但是,当我运行它时,它给了我文件找不到错误。

这是app.jar中的文件列表。 在此处输入图片说明

这是错误消息。

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

我怎么解决这个问题? PS。 在日食中效果很好!

添加。 这是我的Dockerfile。

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

这是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);
    }
    ...
}

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

这是Java传达档案中资源类加载的标准方法。
之后是什么! app.jar文件中。
classes! 但这位于归档文件(jar)中的目录内。

您的问题在那里:

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

您传递的rootPathString路径,但包含在存档( jar )中, FileInputStream无法访问它。
您需要从档案中获取输入流,并将其传递给Properties.load()方法:

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

请注意,当您使用Spring时,您也可以这样做:

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

通过@PropertySource@ConfigurationProperties甚至可以自动加载属性:

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

    private String foo;
    private String bar;

    // getters and setters
}

并在您需要的任何地方注入。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM