[英]Spring-boot - application properties configuration for log file per level
[英]Why is ! in the properties file path in spring-boot application on the docker?
我開發了其余的API。 因此,我嘗試在docker上運行它。 但是,當我運行它時,它給了我文件找不到錯誤。
這是錯誤消息。
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));
您傳遞的rootPath
是String
路徑,但包含在存檔( 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.