ApplicationContextUtil.java
866 Bytes
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
package com.diligrp.website.util.spring;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
* ApplicationContext 普通java程序可以从此处获取bean
*
*/
public class ApplicationContextUtil implements ApplicationContextAware {
private static ApplicationContext context;
public void setApplicationContext(ApplicationContext ctx)
throws BeansException {
context = ctx;
}
public static ApplicationContext getContext() {
return context;
}
@SuppressWarnings("unchecked")
public static <T> T getBean(String name) {
if (context != null) {
return (T) context.getBean(name);
}
return null;
}
public static <T> T getBean(Class<T> clz) {
if (context != null) {
return context.getBean(clz);
}
return null;
}
}