|
| 1 | +package com.code.repository.util; |
| 2 | + |
| 3 | +import org.springframework.beans.factory.config.BeanDefinition; |
| 4 | +import org.springframework.beans.factory.support.BeanDefinitionBuilder; |
| 5 | +import org.springframework.beans.factory.support.DefaultListableBeanFactory; |
| 6 | +import org.springframework.beans.factory.support.GenericBeanDefinition; |
| 7 | +import org.springframework.context.ApplicationContext; |
| 8 | +import org.springframework.context.ApplicationContextAware; |
| 9 | +import org.springframework.context.ConfigurableApplicationContext; |
| 10 | + |
| 11 | +import java.util.Map; |
| 12 | + |
| 13 | +/** |
| 14 | + * Spring容器工具类 |
| 15 | + * 注入bean,获取bean操作 |
| 16 | + */ |
| 17 | +public class SpringContextUtils implements ApplicationContextAware { |
| 18 | + |
| 19 | + // spring容器 |
| 20 | + private static ApplicationContext applicationContext; |
| 21 | + |
| 22 | + public static void init(ApplicationContext context) { |
| 23 | + applicationContext = context; |
| 24 | + } |
| 25 | + |
| 26 | + public void setApplicationContext(ApplicationContext context) { |
| 27 | + applicationContext = context; |
| 28 | + } |
| 29 | + |
| 30 | + public static <T> T getBean(Class<T> clazz) { |
| 31 | + return applicationContext.getBean(clazz); |
| 32 | + } |
| 33 | + |
| 34 | + public static Object getBean(String name) { |
| 35 | + return applicationContext.getBean(name); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * 注册bean到spring |
| 40 | + */ |
| 41 | + public static void registerBean(String name, Object bean) { |
| 42 | + |
| 43 | + ConfigurableApplicationContext context = (ConfigurableApplicationContext) applicationContext; |
| 44 | + // 根据bean上对应的注解,通过spring容器注入依赖 |
| 45 | + context.getBeanFactory().autowireBean(bean); |
| 46 | + // 将当前对象注入到spirng容器中 |
| 47 | + context.getBeanFactory().registerSingleton(name, bean); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * 避免重复注册bean到spring容器 |
| 52 | + */ |
| 53 | + public static void registerIfNotExist(Class<?> clazz) { |
| 54 | + |
| 55 | + // boolean includeNonSingletons, boolean allowEagerInit |
| 56 | + Map<String, ?> beanMap = applicationContext.getBeansOfType(clazz, false, false); |
| 57 | + |
| 58 | + String beanId = springDefaultBeanName(clazz.getSimpleName()); |
| 59 | + |
| 60 | + // 判断是否已存在相同ID |
| 61 | + if (beanMap.size() == 1 || beanMap.containsKey(beanId)) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + registerBean(beanId, clazz); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * 从spring容器中获取bean,如果存在一个直接取出,如果存在多个,根据spring默认的bean名获取 |
| 70 | + */ |
| 71 | + public static <T> T getMatchedBean(Class<T> clazz) { |
| 72 | + T beanObj = null; |
| 73 | + |
| 74 | + // boolean includeNonSingletons, boolean allowEagerInit |
| 75 | + Map<String, T> beanMap = applicationContext.getBeansOfType(clazz, false, false); |
| 76 | + |
| 77 | + String beanId = springDefaultBeanName(clazz.getSimpleName()); |
| 78 | + |
| 79 | + if (beanMap.size() == 1) { |
| 80 | + beanObj = applicationContext.getBean(clazz); |
| 81 | + } else { |
| 82 | + beanObj = beanMap.get(beanId); |
| 83 | + } |
| 84 | + |
| 85 | + return beanObj; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * spring默认beanName |
| 90 | + */ |
| 91 | + public static String springDefaultBeanName(String classSimpleName) { |
| 92 | + char[] ch = classSimpleName.toCharArray(); |
| 93 | + |
| 94 | + //判断首字母大写且第二个字母是小写时,将首字母转换成小写 |
| 95 | + if (ch[0] >= 'A' && ch[0] <= 'Z' && ch[1] >= 'a' && ch[1] <= 'z') { |
| 96 | + ch[0] = (char) (ch[0] + 32); |
| 97 | + } |
| 98 | + return new String(ch); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * 注册class到spring |
| 103 | + */ |
| 104 | + public static void registerBean(String id, Class<?> clazz) { |
| 105 | + |
| 106 | + ConfigurableApplicationContext context = (ConfigurableApplicationContext) applicationContext; |
| 107 | + DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) context.getBeanFactory(); |
| 108 | + |
| 109 | + // 注册对象到spring容器中 |
| 110 | + BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(clazz); |
| 111 | + // dataSourceBuider.addPropertyValue("msg", "hello "); |
| 112 | + beanFactory.registerBeanDefinition(id, beanDefinitionBuilder.getBeanDefinition()); |
| 113 | + |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * 注册className到spring |
| 118 | + */ |
| 119 | + public static void registerBean(String id, String className) { |
| 120 | + ConfigurableApplicationContext context = (ConfigurableApplicationContext) applicationContext; |
| 121 | + DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) context.getBeanFactory(); |
| 122 | + |
| 123 | + // 注册对象到spring容器中 |
| 124 | + BeanDefinition beanDefinition = new GenericBeanDefinition(); |
| 125 | + beanDefinition.setBeanClassName(className); |
| 126 | + // beanDefinition.getPropertyValues().add("msg", "hello "); |
| 127 | + beanFactory.registerBeanDefinition(id, beanDefinition); |
| 128 | + } |
| 129 | + |
| 130 | +} |
0 commit comments