获取Spring 容器中bean的方法

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" />

原文出处:http://blog.csdn.net/lklinkang/article/details/7276894

原文地址:https://www.cnblogs.com/fhtwins/p/2971810.html