com.hs.mail.container.config.ComponentManager.java Source code

Java tutorial

Introduction

Here is the source code for com.hs.mail.container.config.ComponentManager.java

Source

/*
 * Copyright 2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.hs.mail.container.config;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * This class provides an application-wide access to the Spring
 * ApplicationContext!
 * 
 * @author Won Chul Doh
 * @since Feb 3, 2010
 * 
 */
public class ComponentManager implements ApplicationContextAware {

    private static ApplicationContext context;

    public void setApplicationContext(ApplicationContext appContext) throws BeansException {
        context = appContext;
    }

    /**
     * This is about the same as context.getBean("beanName"), except it has its
     * own static handle to the Spring context, so calling this method
     * statically will give access to the beans by name in the Spring
     * application context. As in the context.getBean("beanName") call, the
     * caller must cast to the appropriate target class. If the bean does not
     * exist, then a Runtime error will be thrown.
     * 
     * @param beanName
     *            the name of the bean to get.
     * @return an Object reference to the named bean.
     */
    public static Object getBean(String beanName) throws BeansException {
        return context.getBean(beanName);
    }

    public static Object getBeanOfType(Class type) throws BeansException {
        return BeanFactoryUtils.beanOfType(context, type);
    }

}