java获取spring bean里面的properties文件属性

1.spring配置文件添加一个bean:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<bean id="threadPropertyConfigurer"
class="com.sanmina.mes.dashboard.server.util.ThreadPropertyConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="ignoreResourceNotFound" value="true"/>
<property name="locations">
<list>
<value>WEB-INF/thread.properties</value>
</list>
</property>
</bean>

2.bean类:
ThreadPropertyConfigurer.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package com.sanmina.mes.dashboard.server.util;
import java.io.File;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.util.PropertyPlaceholderHelper;
import com.sanmina.mes.dashboard.server.servlet.ServerApplicationContext;
/**
* instruction: Use For Setting Preference Configuration Cache Refresh Time
*/
public class ThreadPropertyConfigurer extends PropertyPlaceholderConfigurer
{
private static Map<String,String> properties = new HashMap<String,String>();
public static long lastModifyTime = setLastModifyTime() ;
public static long currentTime = System.currentTimeMillis() ;
public static long setLastModifyTime() {
File file = new File("/home/jalenchu/jalen_chu/Tools/server/eclipse/workspace/mes-dashboard-server/dashboardServerWeb/src/main/webapp/WEB-INF/thread.properties");
Calendar calendar = Calendar.getInstance();
long time = file.lastModified();
calendar.setTimeInMillis(time);
lastModifyTime = calendar.getTimeInMillis();
return calendar.getTimeInMillis();
}
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess,Properties props) throws BeansException
{
System.out.println("beanFactoryToProcess:"+beanFactoryToProcess);
System.out.println("props:"+props);
PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper(DEFAULT_PLACEHOLDER_PREFIX, DEFAULT_PLACEHOLDER_SUFFIX, DEFAULT_VALUE_SEPARATOR, false);
for(Entry<Object,Object> entry:props.entrySet()){
String stringKey = String.valueOf(entry.getKey());
String stringValue = String.valueOf(entry.getValue());
stringValue = helper.replacePlaceholders(stringValue, props);
properties.put(stringKey, stringValue);
}
super.processProperties(beanFactoryToProcess, props);
}
public static Map<String, String> getProperties()
{
return properties;
}
public String getProperty(String key)
{
return properties.get(key);
}
}

3.调用方法:

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
private static int getThreadRefreshTime()
{
int refreshTime = 5;
try {
ThreadPropertyConfigurer threadProperties = new ThreadPropertyConfigurer();
String refreshTimeStr = threadProperties.getProperty("threadRefreshTime");
refreshTime = Integer.parseInt(refreshTimeStr);
} catch (Exception e) {
e.printStackTrace();
}
return refreshTime;
}
public static final int REFREASH_MINUTES = getThreadRefreshTime();

4.附录:
thread.properties

1
threadRefreshTime=1234