com.soolr.core.utils.SpringContextHolder.java Source code

Java tutorial

Introduction

Here is the source code for com.soolr.core.utils.SpringContextHolder.java

Source

/**
 * @(#)SpringContextHolder.java 2010-7-6
 * 
 * Copyright 2000-2010 by UFida Corporation.
 *
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * UFida Corporation ("Confidential Information").  You shall not 
 * disclose such Confidential Information and shall use it only in 
 * accordance with the terms of the license agreement you entered 
 * into with UFida.
 */

package com.soolr.core.utils;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class SpringContextHolder implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        SpringContextHolder.applicationContext = applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        checkApplicationContext();
        return applicationContext;
    }

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

    public static <T> T getBean(Class<T> clazz) {
        checkApplicationContext();
        T bean = applicationContext.getBean(clazz);
        return bean;
    }

    private static void checkApplicationContext() {
        if (applicationContext == null) {
            throw new IllegalStateException(
                    "applicaitonContext,applicationContext.xmlSpringContextUtil");
        }
    }

}