acromusashi.stream.helper.SpringContextHelper.java Source code

Java tutorial

Introduction

Here is the source code for acromusashi.stream.helper.SpringContextHelper.java

Source

/**
* Copyright (c) Acroquest Technology Co, Ltd. All Rights Reserved.
* Please read the associated COPYRIGHTS file for more details.
*
* THE SOFTWARE IS PROVIDED BY Acroquest Technolog Co., Ltd.,
* WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDER BE LIABLE FOR ANY
* CLAIM, DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
* OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*/
package acromusashi.stream.helper;

import java.io.Serializable;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;

/**
 * SpringContext????
 * 
 * @author kimura
 */
public class SpringContextHelper implements Serializable {
    /** serialVersionUID */
    private static final long serialVersionUID = -3865604065504036049L;

    /** ApplicationContext?transient?????? */
    private AbstractApplicationContext appContext;

    /** ApplicationContextPath */
    private String appContextPath;

    /**
     * ????
     * 
     * @param appContextPath 
     */
    public SpringContextHelper(String appContextPath) {
        this.appContextPath = appContextPath;
    }

    /**
     * ?????????
     * 
     * @param componentType ??
     * 
     * @param <T> ??????
     * @return ???
     */
    public <T> T getComponent(Class<T> componentType) {
        BeanFactory factory = getBeanFactory();
        T component = factory.getBean(componentType);
        return component;
    }

    /**
     * ????????????
     * 
     * @param componentName ????
     * @param componentType ??
     * 
     * @param <T> ??????
     * @return ???
     */
    public <T> T getComponent(String componentName, Class<T> componentType) {
        BeanFactory factory = getBeanFactory();
        T component = factory.getBean(componentName, componentType);
        return component;
    }

    /**
     * BeanFactory??<br>
     * ?Context?????????Context??BeanFactory??<br>
     * ????????Context????
     * 
     * @return BeanFactory
     */
    protected BeanFactory getBeanFactory() {
        if (this.appContext == null) {
            this.appContext = new GenericXmlApplicationContext(this.appContextPath);
        }

        return this.appContext.getBeanFactory();
    }

    /**
     * @return the appContext
     */
    public AbstractApplicationContext getAppContext() {
        return this.appContext;
    }

    /**
     * @param appContext the appContext to set
     */
    public void setAppContext(AbstractApplicationContext appContext) {
        this.appContext = appContext;
    }
}