| New file | 
 |  |  | 
 |  |  | package com.dy.common.springUtil; | 
 |  |  |  | 
 |  |  | import org.springframework.beans.BeansException; | 
 |  |  | import org.springframework.beans.MutablePropertyValues; | 
 |  |  | import org.springframework.beans.factory.support.DefaultListableBeanFactory; | 
 |  |  | import org.springframework.beans.factory.support.GenericBeanDefinition; | 
 |  |  | import org.springframework.context.ApplicationContext; | 
 |  |  | import org.springframework.context.ApplicationContextAware; | 
 |  |  | import org.springframework.stereotype.Component; | 
 |  |  |  | 
 |  |  | import java.util.Map; | 
 |  |  |  | 
 |  |  | /** | 
 |  |  |  * 获取Springboot上下文,并通过上下文可以获取配置文件中的配置参数。 | 
 |  |  |  * SpringBoot启动时,发现本类注解了@Component,会加载本类并实例化, | 
 |  |  |  * 实例化时发现本类实现了接口ApplicationContextAware,则会调用 | 
 |  |  |  * setApplicationContext方法设置Spring上下文对象 | 
 |  |  |  */ | 
 |  |  | @Component | 
 |  |  | public class SpringContextUtil implements ApplicationContextAware { | 
 |  |  |  | 
 |  |  |     private static ApplicationContext applicationContext = null; | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { | 
 |  |  |         if (SpringContextUtil.applicationContext == null) { | 
 |  |  |             SpringContextUtil.applicationContext = applicationContext; | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     //获取applicationContext | 
 |  |  |     public static ApplicationContext getApplicationContext() { | 
 |  |  |         return applicationContext; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     //通过name获取 Bean. | 
 |  |  |     public static Object getBean(String name) { | 
 |  |  |         return getApplicationContext().getBean(name); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     //通过class获取Bean. | 
 |  |  |     public static <T> T getBean(Class<T> clazz) { | 
 |  |  |         return getApplicationContext().getBean(clazz); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 同步方法注册bean到ApplicationContext中 | 
 |  |  |      * | 
 |  |  |      * @param beanName | 
 |  |  |      * @param clazz | 
 |  |  |      * @param original bean的属性值 | 
 |  |  |      */ | 
 |  |  |     public static synchronized void setBean(String beanName, Class<?> clazz, Map<String,Object> original) { | 
 |  |  |         //checkApplicationContext(); | 
 |  |  |         DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) applicationContext.getAutowireCapableBeanFactory(); | 
 |  |  |         if(beanFactory.containsBean(beanName)){ | 
 |  |  |             return; | 
 |  |  |         } | 
 |  |  |         //BeanDefinition beanDefinition = new RootBeanDefinition(clazz); | 
 |  |  |         GenericBeanDefinition definition = new GenericBeanDefinition(); | 
 |  |  |         //类class | 
 |  |  |         definition.setBeanClass(clazz); | 
 |  |  |         //属性赋值 | 
 |  |  |         definition.setPropertyValues(new MutablePropertyValues(original)); | 
 |  |  |         //注册到spring上下文 | 
 |  |  |         beanFactory.registerBeanDefinition(beanName, definition); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  | } |