1
zj
2025-01-15 d5276168f8dfb9b4d41819a33b707f1ce083cb77
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
package kernel.springframework;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class ServiceLocator {
    /**
     * spring应用上下文
     */
    private static ApplicationContext CONTEXT;
 
    /**
     * Method getApplicationContext.
     *
     * @return ApplicationContext
     */
    public static ApplicationContext getApplicationContext() {
        if (CONTEXT == null) {
            CONTEXT = new ClassPathXmlApplicationContext(new String[] { "classpath*:spring/*.xml" });
        }
        return CONTEXT;
    }
 
    /**
     * Method setApplicationContext.
     *
     * @param outcontext ApplicationContext
     */
    public static void setApplicationContext(ApplicationContext outcontext) {
        CONTEXT = outcontext;
    }
 
    /**
     * 获得一个Spring的Bean对象
     *
     * @param name
     * @return
     */
    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }
 
    /**
     * 通过calss 获得一个Spring的Bean对象
     *
     * @param T
     * @return
     */
    public static <T> T getBean(Class<T> T) {
        return getApplicationContext().getBean(T);
    }
    
    /**
     * 
     * @param key :properties定义的值
     * @param defaultMessage:取不到值,默认的值
     * @return
     */
    public static String getMessage(String key,String defaultMessage){
        return CONTEXT.getMessage(key, null, defaultMessage,null);
    }
    
    /**
     * 
     * @param key :properties定义的值
     * @return  如果key值 没有,将返回"",
     */
    public static String getMessage(String key){
        return CONTEXT.getMessage(key, null,"",null);
    }
}