com.wsun.seap.common.utils.SpringContextHolder.java Source code

Java tutorial

Introduction

Here is the source code for com.wsun.seap.common.utils.SpringContextHolder.java

Source

/**
 * Copyright &copy; 2012-2013 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.wsun.seap.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

/**
 * ?????Spring ApplicationContext, ???ApplicaitonContext.
 *
 * @author Zaric
 * @date 2013-5-29 ?1:25:40
 */
@Service
@Lazy(false)
public class SpringContextHolder implements ApplicationContextAware {

    private static ApplicationContext applicationContext = null;

    private static Logger logger = LoggerFactory.getLogger(SpringContextHolder.class);

    /**
     * ?????ApplicationContext.
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * ????applicationContext?Bean, .
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) {
        return (T) applicationContext.getBean(name);
    }

    /**
     * ????applicationContext?Bean, .
     */
    public static <T> T getBean(Class<T> requiredType) {
        return applicationContext.getBean(requiredType);
    }

    /**
     * ApplicationContextAware?, Context????.
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {

        if (SpringContextHolder.applicationContext != null) {
            logger.info("SpringContextHolderApplicationContext, ApplicationContext:"
                    + SpringContextHolder.applicationContext);
        }

        SpringContextHolder.applicationContext = applicationContext; // NOSONAR
    }

}