Spring boot 由于多个感叹号,SpringBoot无法从可运行jar加载属性文件
我有一个Spring Boot项目,在本地一切正常。现在,当我通过jenkins创建一个可运行的jar来运行它时,它就无法加载属性文件。 以下是配置PropertyPlaceholder的代码:Spring boot 由于多个感叹号,SpringBoot无法从可运行jar加载属性文件,spring-boot,properties,Spring Boot,Properties,我有一个Spring Boot项目,在本地一切正常。现在,当我通过jenkins创建一个可运行的jar来运行它时,它就无法加载属性文件。 以下是配置PropertyPlaceholder的代码: @Bean public static EncryptablePropertyPlaceholderConfigurer propertyPlaceholderConfigurerEncrypted() { String env = System.getProperty(&quo
@Bean
public static EncryptablePropertyPlaceholderConfigurer propertyPlaceholderConfigurerEncrypted() {
String env = System.getProperty("spring.profiles.active") != null ? System.getProperty("spring.profiles" +
".active") : "ci";
EncryptablePropertyPlaceholderConfigurer ppc =
new EncryptablePropertyPlaceholderConfigurer(getStandardPBEStringEncryptor());
ppc.setLocations(new ClassPathResource("application.properties"),
new ClassPathResource("application-" + env + ".properties"));
return ppc;
}
为了调试,我在其中添加了以下代码:
try {
String s = new String(Files.readAllBytes(new ClassPathResource("application-" + env + ".properties").getFile().toPath()));
LOG.info(s);
} catch (IOException e) {
LOG.error("Unable to read file",e);
}
它给出了这个错误:
java.io.FileNotFoundException: class path resource [application-qa1.properties] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/var/hudson/workspace/pv/target/T-S-21.3.40.jar!/BOOT-INF/classes!/application-qa1.properties
17:23:44 at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:217)
我已经确认文件位于jar中的这个位置BOOT-INF/classes/application-qa1.properties
所以,这个问题实际上是由于从jar/var/hudson/workspace/pv/target/T-S-21.3.40.jar加载文件时路径中出现了第二个感叹号引起的/BOOT-INF/classes/应用程序-qa1.属性
理想情况下,感叹号应该只出现在jar名称之后
有人能告诉你如何解决这个问题吗。你不能从这样的罐子里读取文件。您必须像这样使用getResourceAsStream:
InputStream is = this.getClass().getClassLoader().getResourceAsStream("application-" + env + ".properties");