001 /*
002 * Copyright 2010 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package com.hs.mail.container.config;
017
018 import org.springframework.beans.BeansException;
019 import org.springframework.beans.factory.BeanFactoryUtils;
020 import org.springframework.context.ApplicationContext;
021 import org.springframework.context.ApplicationContextAware;
022
023 /**
024 * This class provides an application-wide access to the Spring
025 * ApplicationContext!
026 *
027 * @author Won Chul Doh
028 * @since Feb 3, 2010
029 *
030 */
031 public class ComponentManager implements ApplicationContextAware {
032
033 private static ApplicationContext context;
034
035 public void setApplicationContext(ApplicationContext appContext)
036 throws BeansException {
037 context = appContext;
038 }
039
040 /**
041 * This is about the same as context.getBean("beanName"), except it has its
042 * own static handle to the Spring context, so calling this method
043 * statically will give access to the beans by name in the Spring
044 * application context. As in the context.getBean("beanName") call, the
045 * caller must cast to the appropriate target class. If the bean does not
046 * exist, then a Runtime error will be thrown.
047 *
048 * @param beanName
049 * the name of the bean to get.
050 * @return an Object reference to the named bean.
051 */
052 public static Object getBean(String beanName) throws BeansException {
053 return context.getBean(beanName);
054 }
055
056 public static Object getBeanOfType(Class type) throws BeansException {
057 return BeanFactoryUtils.beanOfType(context, type);
058 }
059
060 }