123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- package com.jtgh.yjpt.common;
- import org.springframework.beans.BeansException;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.ApplicationContextAware;
- /**
- * 获取spring初始化的applicationContext
- *
- * @author masn
- *
- */
- public class ApplicationContextHelper implements ApplicationContextAware {
- private static ApplicationContext appCtx;
- /**
- * 此方法可以把ApplicationContext对象inject到当前类中作为一个静态成员变量。
- *
- * @param applicationContext
- * ApplicationContext 对象.
- * @throws BeansException
- */
- @Override
- public void setApplicationContext(ApplicationContext applicationContext)
- throws BeansException {
- appCtx = applicationContext;
- }
- /**
- * 快速得到一个BEAN的方法
- *
- * @param beanName
- * bean的名字
- * @return 返回一个bean对象
- */
- public static Object getBean(String beanName) {
- return appCtx.getBean(beanName);
- }
- /**
- * 快速得到一个BEAN的方法
- *
- * @param beanName
- * bean的名字
- * @return 返回一个bean对象
- */
- public static Object getBean(Class<?> c) {
- return appCtx.getBean(c);
- }
- }
|