Skip to content

Commit

Permalink
[ISSUE-alibaba#4258] Fix wrong path when -Dspring.config.location not…
Browse files Browse the repository at this point in the history
… set (alibaba#4259)

* fix spring.config.location is nullapplication.properties when -Dspring.config.location is not set in env

* fix wrong path when -Dspring.config.location not set

* modify default file resource method name

* fix "/" magic value

* change the way of get file

* not judge pathSplit by self, use Paths.get(a, b);

* when spring.config.location is not set, use {nacos.home}/conf/application.properties to cover it.

* refactor code

* code quality enhance

* just use two level to load conf. {spring.config.location}/application.properties -> classpath:/application.properties

* code clean
  • Loading branch information
horizonzy committed Nov 25, 2020
1 parent 6ad8da4 commit dc266b0
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions sys/src/main/java/com/alibaba/nacos/sys/env/EnvUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -361,19 +361,31 @@ public static String getMemberList() {
private static final String FILE_PREFIX = "file:";

public static Resource getApplicationConfFileResource() {
Resource customResource = getCustomFileResource();
return customResource == null ? getDefaultResource() : customResource;
}

private static Resource getCustomFileResource() {
String path = getProperty("spring.config.location");
InputStream inputStream = null;
if (StringUtils.isNotBlank(path) && path.contains(FILE_PREFIX)) {
String[] paths = path.split(",");
path = paths[paths.length - 1].substring(FILE_PREFIX.length());
return getRelativePathResource(path, "application.properties");
}
return null;
}

private static Resource getRelativePathResource(String parentPath, String path) {
try {
inputStream = new FileInputStream(new File(path + "application.properties"));
InputStream inputStream = new FileInputStream(Paths.get(parentPath, path).toFile());
return new InputStreamResource(inputStream);
} catch (Exception ignore) {
}
if (inputStream == null) {
inputStream = EnvUtil.class.getResourceAsStream("/application.properties");
}
return null;
}

private static Resource getDefaultResource() {
InputStream inputStream = EnvUtil.class.getResourceAsStream("/application.properties");
return new InputStreamResource(inputStream);
}

Expand Down

0 comments on commit dc266b0

Please sign in to comment.