1
package com.ycb.app.provider.utils;
2
3
import org.springframework.beans.BeansException;
4
import org.springframework.core.io.support.PropertiesLoaderUtils;
5
6
import java.io.IOException;
7
import java.io.UnsupportedEncodingException;
8
import java.util.HashMap;
9
import java.util.Map;
10
import java.util.Properties;
11
12
public class PropertyUtil {
13
private static Map<String, String> propertiesMap = new HashMap<>();
14
// Default as in PropertyPlaceholderConfigurer
15
16
17
public static void processProperties( Properties props) throws BeansException {
18
19
propertiesMap = new HashMap<String, String>();
20
for (Object key : props.keySet()) {
21
String keyStr = key.toString();
22
23
try {
24
//PropertiesLoaderUtils的默认编码是ISO-8859-1,在这里转码一下
25
propertiesMap.put(keyStr, new String(props.getProperty(keyStr).getBytes("ISO-8859-1"),"utf-8"));
26
} catch (UnsupportedEncodingException e) {
27
e.printStackTrace();
28
}catch (java.lang.Exception e){
29
e.printStackTrace();
30
};
31
}
32
System.out.println(propertiesMap);
33
}
34
public static void loadAllProperties(){
35
try {
36
37
Properties properties = PropertiesLoaderUtils.loadAllProperties("bootstrap.properties");
38
processProperties(properties);
39
} catch (IOException e) {
40
e.printStackTrace();
41
}
42
}
43
44
public static String getProperty(String name) {
45
return propertiesMap.get(name).toString();
46
}
47
48
public static void main(String[] args) {
49
System.out.println("获取配置文件属性"+PropertyUtil.getProperty("spring.application.name"));
50
}
51
}
52
1
package com.ycb.app.provider;
2
3
import com.ycb.app.provider.listener.ApplicationStartUpListener;
4
import org.springframework.boot.SpringApplication;
5
import org.springframework.boot.autoconfigure.SpringBootApplication;
6
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
7
8
9
10
11
public class AppProviderApplication {
12
public static void main(String[] args) {
13
SpringApplication application = new SpringApplication(AppProviderApplication.class);
14
application.addListeners(new ApplicationStartUpListener());
15
application.run(args);
16
}
17
}
18
19
1
package com.ycb.app.provider.listener;
2
3
import com.ycb.app.provider.utils.PropertyUtil;
4
import org.springframework.boot.context.event.ApplicationStartedEvent;
5
import org.springframework.context.ApplicationListener;
6
7
public class ApplicationStartUpListener implements ApplicationListener<ApplicationStartedEvent> {
8
9
10
11
public void onApplicationEvent(ApplicationStartedEvent event) {
12
PropertyUtil.loadAllProperties();
13
System.out.println("ApplicationStartUpListener EXEC");
14
}
15
}
1
package com.ycb.app.provider.utils;
2
3
import org.springframework.beans.BeansException;
4
import org.springframework.core.io.support.PropertiesLoaderUtils;
5
6
import java.io.IOException;
7
import java.io.UnsupportedEncodingException;
8
import java.util.HashMap;
9
import java.util.Map;
10
import java.util.Properties;
11
12
public class PropertyUtil {
13
private static Map<String, String> propertiesMap = new HashMap<>();
14
// Default as in PropertyPlaceholderConfigurer
15
16
17
public static void processProperties( Properties props) throws BeansException {
18
19
propertiesMap = new HashMap<String, String>();
20
for (Object key : props.keySet()) {
21
String keyStr = key.toString();
22
23
try {
24
//PropertiesLoaderUtils的默认编码是ISO-8859-1,在这里转码一下
25
propertiesMap.put(keyStr, new String(props.getProperty(keyStr).getBytes("ISO-8859-1"),"utf-8"));
26
} catch (UnsupportedEncodingException e) {
27
e.printStackTrace();
28
}catch (java.lang.Exception e){
29
e.printStackTrace();
30
};
31
}
32
System.out.println(propertiesMap);
33
}
34
public static void loadAllProperties(){
35
try {
36
37
Properties properties = PropertiesLoaderUtils.loadAllProperties("bootstrap.properties");
38
processProperties(properties);
39
} catch (IOException e) {
40
e.printStackTrace();
41
}
42
}
43
44
public static String getProperty(String name) {
45
return propertiesMap.get(name).toString();
46
}
47
48
public static void main(String[] args) {
49
System.out.println("获取配置文件属性"+PropertyUtil.getProperty("spring.application.name"));
50
}
51
}
52
1
package com.ycb.app.provider;
2
3
import com.ycb.app.provider.listener.ApplicationStartUpListener;
4
import org.springframework.boot.SpringApplication;
5
import org.springframework.boot.autoconfigure.SpringBootApplication;
6
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
7
8
9
10
11
public class AppProviderApplication {
12
public static void main(String[] args) {
13
SpringApplication application = new SpringApplication(AppProviderApplication.class);
14
application.addListeners(new ApplicationStartUpListener());
15
application.run(args);
16
}
17
}
18
19
1
package com.ycb.app.provider.listener;
2
3
import com.ycb.app.provider.utils.PropertyUtil;
4
import org.springframework.boot.context.event.ApplicationStartedEvent;
5
import org.springframework.context.ApplicationListener;
6
7
public class ApplicationStartUpListener implements ApplicationListener<ApplicationStartedEvent> {
8
9
10
11
public void onApplicationEvent(ApplicationStartedEvent event) {
12
PropertyUtil.loadAllProperties();
13
System.out.println("ApplicationStartUpListener EXEC");
14
}
15
}