在编写Web代码,可能需要用到工具类或者没有加入到Spring Bean工厂的类,能够访问到ApplicationContext、bean和配置项,这时,因为该类没有加入到Spring容器中,不能使用@Autowired等注解来注入Spring中的Bean,这是可以通过一个工具类完成这件事。
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import javax.servlet.ServletContext;
import java.util.Collections;
import java.util.Map;
@Configuration
public class ApplicationContextExt implements ApplicationContextAware {
private static ApplicationContext context = null;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ApplicationContextExt.context = applicationContext;
}
/**
* 取得存储在静态变量中的ApplicationContext.
*/
public static ApplicationContext getContext() {
if (context == null)
throw new IllegalStateException("can not found application context.");
return context;
}
/**
* 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型
*/
@SuppressWarnings("unchecked")
public static <T> T getBean(String name) {
if (context == null)
throw new IllegalStateException("can not find application context.");
try {
return (T) context.getBean(name);
} catch (BeansException e) {
}
return (T) null;
}
@SuppressWarnings("unchecked")
public static <T> T getBean(String name, ServletContext sc) {
if (sc == null)
throw new IllegalStateException("can not find servlet context.");
try {
return (T) WebApplicationContextUtils.getWebApplicationContext(sc).getBean(name);
} catch (BeansException e) {
}
return (T) null;
}
public static <T> T getBean(Class<T> cls) {
if (context == null)
throw new IllegalStateException("can not find application context.");
try {
return (T) context.getBean(cls);
} catch (BeansException e) {
}
return (T) null;
}
public static <T> T getBean(Class<T> cls, ServletContext sc) {
if (sc == null)
throw new IllegalStateException("can not find servlet context.");
try {
return (T) WebApplicationContextUtils.getWebApplicationContext(sc).getBean(cls);
} catch (BeansException e) {
}
return (T) null;
}
public static <T> Map<String, T> getBeansOfType(Class<T> cls) {
if (context == null)
throw new IllegalStateException("can not found application context.");
try {
return context.getBeansOfType(cls);
} catch (BeansException e) {
}
return Collections.emptyMap();
}
public static <T> Map<String, T> getBeansOfType(Class<T> cls, ServletContext sc) {
if (sc == null) {
throw new IllegalStateException("can not find servlet context.");
}
try {
return WebApplicationContextUtils.getWebApplicationContext(sc).getBeansOfType(cls);
} catch (BeansException e) {
// TODO: handle exception
}
return Collections.emptyMap();
}
/**
* 根据WebApp的虚拟路径获取文件的绝对路径
*
* @param path
* @return
*/
public static String getAbsolutePathInWeb(String path) {
return ((WebApplicationContext) context).getServletContext().getRealPath(path);
}
public static Resource getResource(String location) {
return context.getResource(location);
}
}
比如:
策略工厂中
获取到Bean
ApplicationContextExt.getBean(AService.class);