forked from spring-cloud/spring-cloud-commons
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMyPropertySourceLocator.java
41 lines (36 loc) · 1.31 KB
/
MyPropertySourceLocator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package cn.haitaoss.BootstrapProperties.BootstrapConfiguration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertySource;
import javax.annotation.Resource;
import java.io.IOException;
/**
* @author haitao.chen
* email haitaoss@aliyun.com
* date 2023-03-15 15:04
*
*/
public class MyPropertySourceLocator implements PropertySourceLocator {
public MyPropertySourceLocator() {
System.out.println("MyPropertySourceLocator...构造器");
}
@Resource
private ApplicationContext applicationContext;
@Value("${dynamicConfigFile}")
private String filePath;
@Override
public PropertySource<?> locate(Environment environment) {
PropertySource<?> propertySource;
try {
// 也可以是网络资源
propertySource = new YamlPropertySourceLoader().load("haitao-propertySource",
applicationContext.getResource(filePath)).get(0);
} catch (IOException e) {
throw new RuntimeException(e);
}
return propertySource;
}
}