Spring中ApplicationContextAware接口的作用

来源:互联网 发布:土豆客户端淘宝登录 编辑:程序博客网 时间:2024/05/29 07:02

加载Spring配置文件时,如果Spring配置文件中所定义的Bean类实现了ApplicationContextAware 接口,那么在加载Spring配置文件时,会自动调用ApplicationContextAware 接口中的

public void setApplicationContext(ApplicationContext context) throws BeansException

方法,获得ApplicationContext对象。

前提必须在Spring配置文件中指定该类

**
 * ApplicationContextRegister
 *
 * @author yangyy
 * @message Created by yangyy on 16/11/3.
 */
public class ApplicationContextRegister implements ApplicationContextAware {

    private static final Logger _logger = LoggerFactory.getLogger(ApplicationContextRegister.class);

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

        _logger.debug("ApplicationContext registed");

        ApplicationContextUtil.getInstance().setApplicationContext(applicationContext);

    }

}

/**
 * ApplicationContextUtil
 *
 * @author yangyy
 * @message Created by yangyy on 16/11/3.
 */
public class ApplicationContextUtil {

    private static final Logger _logger = LoggerFactory.getLogger(ApplicationContextUtil.class);

    private static ApplicationContextUtil instance = null;

    private ApplicationContext applicationContext = null;

    private ApplicationContextUtil() {
    }

    /**
     * Gets instance.
     *
     * @return the instance
     */
    public static ApplicationContextUtil getInstance() {

        if (instance== null) {
            synchronized (ApplicationContextUtil.class) {
                if (instance== null) {
                    instance= new ApplicationContextUtil();
                }
            }
        }

        return instance;
    }

    /**
     * Sets application context.
     *
     * @param applicationContext the application context
     */
    public void setApplicationContext(ApplicationContext applicationContext) {

        synchronized (instance) {
            _logger.debug("setApplicationContext, notifyAll");
            instance.applicationContext = applicationContext;
            instance.notifyAll();
        }
    }

    /**
     * Gets application context.
     *
     * @return the application context
     */
    public ApplicationContext getApplicationContext() {

        synchronized (instance) {

            while (applicationContext == null) {
                try {
                    _logger.debug("getApplicationContext, wait...");

                    instance.wait(20000);

                    if (applicationContext == null) {
                        _logger.error("Have been waiting for ApplicationContext to be set for 20s", new Exception());
                    }

                } catch (InterruptedException ex) {
                    _logger.error("getApplicationContext, wait interrupted", ex);
                }
            }

            return applicationContext;
        }
    }

    /**
     * Gets bean.
     *
     * @param name the name
     * @return the bean
     */
    public <T> T getBean(String name) {
        return (T) getApplicationContext().getBean(name);
    }

    /**
     * Get bean.
     *
     * @param <T> the type parameter
     * @param clazz the clazz
     * @return the t
     */
    public <T> T getBean(Class<T> clazz) {
        return (T) getApplicationContext().getBean(clazz);
    }

配置文件:<bean class="com.yangbw.framework.core.support.ApplicationContextRegister" />

0 0
原创粉丝点击