All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.anonymousmister.springtool.SpringBeanUtil Maven / Gradle / Ivy

package com.github.anonymousmister.springtool;

import lombok.Getter;
import org.reflections.Reflections;
import org.reflections.scanners.FieldAnnotationsScanner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
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;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Stream;

/**
 * @author admin
 * SpringBeanUtil
 * Bean 的工具类
 * 继承它 会自动向子类有
 * 

* Autowired.class * Resource.class * 注解的实体 注入值 如果有的化 */ @Component @Order(Integer.MIN_VALUE) public class SpringBeanUtil implements ApplicationContextAware { private final static Logger log = LoggerFactory.getLogger(SpringBeanUtil.class); @Getter private static ApplicationContext applicationContext; public SpringBeanUtil() { injection(this); } /** * 手动注入 ioc * 默认注解 * Autowired.class * Resource.class * * @param c */ public static boolean injection(Object c) { return injection(c, new Class[]{Autowired.class, Resource.class}); } /** * 手动注入 * * @param c * @param annotations */ public static boolean injection(Object c, Class[] annotations) { if (getApplicationContext() == null || c == null || annotations == null) { return false; } Reflections reflections = new Reflections(c.getClass(), new FieldAnnotationsScanner()); Set fields = new HashSet<>(); Stream.of(annotations).map(o -> { return reflections.getFieldsAnnotatedWith(o); }).filter(Objects::nonNull).forEach(o -> { fields.addAll(o); }); for (Field field : fields) { Object bean = getBean(field); field.setAccessible(true); try { field.set(c, bean); } catch (IllegalAccessException e) { log.error("SpringBeanUtil构造失败", e); e.printStackTrace(); } } return true; } private static Object getBean(Field field) { String name = field.getName(); Object bean = null; try { bean = getApplicationContext().getBean(name); } catch (RuntimeException e) { log.error("查找bean 失败 未找到 名称为" + name + "的实体", e); } if (bean != null) { return bean; } return getApplicationContext().getBean(field.getType()); } /** * 用bean组件的name来获取bean * * @param beanName */ @SuppressWarnings("unchecked") public static T getBean(String beanName) { try { return (T) getApplicationContext().getBean(beanName); } catch (RuntimeException e) { log.error("查找bean 失败 未找到 名称为 [{}] 的实体", beanName, e); } return null; } /** * 用类来获取bean * * @param c */ public static T getBean(Class c) { return (T) getApplicationContext().getBean(c); } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { SpringBeanUtil.applicationContext = applicationContext; } public T classInitialization(Class c, String name, Object... constructorValues) { if (applicationContext.containsBean(name)) { return getBean(name); } ConfigurableApplicationContext bean = getBean(ConfigurableApplicationContext.class); BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(c); if (constructorValues != null) { for (Object constructorValue : constructorValues) { beanDefinitionBuilder.addConstructorArgValue(constructorValue); } } BeanDefinition beanDefinition = beanDefinitionBuilder.getRawBeanDefinition(); BeanDefinitionRegistry beanFactory = (BeanDefinitionRegistry) bean.getBeanFactory(); beanFactory.registerBeanDefinition(name, beanDefinition); return applicationContext.getBean(name, c); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy