1、web工程
ContextLoaderListener.getCurrentWebApplicationContext().getBean("beanName"); //这个太牛逼了!
2、普通JAVA工程
/** * */ public class ApplicationContextUtil implements ApplicationContextAware { private static ApplicationContext context;//声明一个静态变量保存 @SuppressWarnings ("static-access") @Override public void setApplicationContext (ApplicationContext context) throws BeansException { this.context = context; } public static ApplicationContext getContext(){ return context; } }
spring 配置
<bean id="applicationContextUtil" class="net.carefx.platform.mdm.server.core.utils.ApplicationContextUtil" />