import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; /** * 此类可以取得Spring的上下文. * Spring 使new方法创建的对象可以引用spring管理的bean. */ public class SpringContext implements ApplicationContextAware { protected static ApplicationContext context; public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { context = applicationContext; } public static ApplicationContext getContext() { return context; } }
在Spring中配置
<bean id="springContext" lazy-init="false" class="gy.oa.commons.util.SpringContext"></bean>
用法:
SpringContext.getContext().getBean(beanName);