|
@ -3,7 +3,9 @@ package com.yihu.hos.web.framework.util.springutil;
|
|
|
|
|
|
import org.springframework.beans.BeansException;
|
|
|
import org.springframework.context.ApplicationContext;
|
|
|
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
|
|
import org.springframework.context.ApplicationContextAware;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.web.context.ContextLoader;
|
|
|
|
|
|
/**
|
|
|
* 服务管理器。服务管理器是所有顶级服务的生成器。若是单元测试使用此服务工厂,Spring Context会被ServiceFactory创建。
|
|
@ -13,8 +15,8 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
|
|
|
* @version 1.0
|
|
|
* @created 12-05-2015 17:47:55
|
|
|
*/
|
|
|
|
|
|
public class SpringBeanUtil {
|
|
|
@Component
|
|
|
public class SpringBeanUtil implements ApplicationContextAware {
|
|
|
private static ApplicationContext springContext = null;
|
|
|
|
|
|
/**
|
|
@ -26,10 +28,14 @@ public class SpringBeanUtil {
|
|
|
return springContext;
|
|
|
}
|
|
|
|
|
|
public static void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
|
|
springContext = applicationContext;
|
|
|
@Override
|
|
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
|
|
if(SpringBeanUtil.springContext == null) {
|
|
|
SpringBeanUtil.springContext = applicationContext;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
public static <T> T getBean(String beanName){
|
|
|
return (T) springContext.getBean(beanName);
|
|
|
}
|
|
@ -44,11 +50,8 @@ public class SpringBeanUtil {
|
|
|
*/
|
|
|
public static <T> T getService(String serviceName) {
|
|
|
if (springContext == null) {
|
|
|
springContext = new ClassPathXmlApplicationContext(new String[]
|
|
|
{
|
|
|
"spring/applicationContext.xml"
|
|
|
}
|
|
|
);
|
|
|
springContext = ContextLoader.getCurrentWebApplicationContext();
|
|
|
System.out.println("=======springContext=:"+(springContext==null));
|
|
|
}
|
|
|
|
|
|
return (T) springContext.getBean(serviceName);
|