web项目获取spring的applicationContext方式一

web项目获取spring的applicationContext方式一

package com.sz.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class ApplicationContextHelper<T> implements ApplicationContextAware   {

    private static   ApplicationContext context;


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
         context = applicationContext;
    }

    public ApplicationContext getContext() {
        return context;
    }

    public static Object getBean(String beanName) {
        return context.getBean(beanName);
    }

    public static <T> T getBean(Class<T> clz) {
        return context.getBean(clz);

    }

}

posted @ 2018-08-08 18:03  沙漠皇帝  阅读(623)  评论(0编辑  收藏  举报