/* * Copyright 2009 by primedata Corporation. * , * * All rights reserved. * * This software is the confidential and proprietary information of * primedata 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 primedata. */ package com.primeton.dgs.kernel.core.common; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeansException; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.ConfigurableApplicationContext; /** * Spring上下文帮助类,方便获取bean、message等 * * @author user * @version 1.0 Date: 2009-7-7 */ @Slf4j public class SpringContextHelper implements ApplicationContextAware { /** * Spring上下文 */ private static ApplicationContext applicationContext; /** * 获取Spring上下文 * * @return Spring Application Context */ public static ApplicationContext getSpringContext() { return applicationContext; } /* * (non-Javadoc) * * @see org.springframework.context.ApplicationContextAware * #setApplicationContext(org.springframework.context.ApplicationContext) */ public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { SpringContextHelper.applicationContext = applicationContext; } /** * 获取定义在Spring配置文件里的bean实例 * * @param beanName Spring配置文件里的bean注册名 * @return bean实例(没有找到返回null) */ public static T getBean(String beanName) { return (T) applicationContext.getBean(beanName); } /** * 获取类型为requiredType的对象
* 如果bean不能被类型转换,相应的异常将会被抛出(BeanNotOfRequiredTypeException) * * @param beanName Spring配置文件里的bean注册名 * @param requiredType 返回对象类型 * @return bean实例(没有找到返回null) */ public static T getBean(String beanName, Class requiredType) throws BeansException { return applicationContext.getBean(beanName, requiredType); } public static T getBean(Class requiredType) throws BeansException { return applicationContext.getBean(requiredType); } public static T getBean(String name, Object... args) throws BeansException { return (T) applicationContext.getBean(name, args); } public static T getBean(Class requiredType, Object... args) throws BeansException { return applicationContext.getBean(requiredType, args); } /** * 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true * * @param name Spring配置文件里的bean注册名 * @return boolean */ public static boolean containsBean(String name) { return applicationContext.containsBean(name); } /** * 判断以给定名字注册的bean定义是一个singleton还是一个prototype。 * 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException) * * @param name Spring配置文件里的bean注册名 * @return boolean 是否单例 * @throws NoSuchBeanDefinitionException 没有定义Bean */ public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException { return applicationContext.isSingleton(name); } /** * 如果给定的bean名字在bean定义中有别名,则返回这些别名 * * @param name Spring配置文件里的bean注册名 * @return 别名 * @throws NoSuchBeanDefinitionException 没有定义Bean */ public static String[] getAliases(String name) throws NoSuchBeanDefinitionException { return applicationContext.getAliases(name); } /** * 手动注入Spring Bean中 * * @param name BeanName * @param clazz 注册的bean的类性 * @param args 构造方法的必要参数,顺序和类型要求和clazz中定义的一致 * @param * @return 返回注册到容器中的bean对象 */ public static T registerBean(String name, Class clazz, Object... args) { ConfigurableApplicationContext context = null; if (applicationContext instanceof ConfigurableApplicationContext) { context = ((ConfigurableApplicationContext) applicationContext); } log.info("Register {} to spring context...",clazz.getCanonicalName()); if (context != null) { if (context.containsBean(name)) { Object bean = context.getBean(name); if (bean.getClass().isAssignableFrom(clazz)) { return (T) bean; } else { throw new RuntimeException("BeanName 重复 " + name); } } BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(clazz); for (Object arg : args) { beanDefinitionBuilder.addConstructorArgValue(arg); } BeanDefinition beanDefinition = beanDefinitionBuilder.getRawBeanDefinition(); BeanDefinitionRegistry beanFactory = (BeanDefinitionRegistry) context.getBeanFactory(); beanFactory.registerBeanDefinition(name, beanDefinition); return context.getBean(name, clazz); } return null; } }