新增部分工具
This commit is contained in:
parent
b5bc5b883e
commit
38416ff420
@ -0,0 +1,16 @@
|
||||
package com.hxuanyu.common.config;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* common 自动配置类
|
||||
*
|
||||
* @author hanxuanyu
|
||||
* @version 1.0
|
||||
*/
|
||||
@Configuration
|
||||
@ComponentScan("com.hxuanyu.common")
|
||||
public class CommonConfiguration {
|
||||
|
||||
}
|
@ -0,0 +1,92 @@
|
||||
package com.hxuanyu.common.spring;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
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.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* SpringContext工具类,用于获取全局ApplicationContext以及从中获取bean
|
||||
*
|
||||
* @author hanxuanyu
|
||||
* @version 1.0
|
||||
*/
|
||||
@Component
|
||||
@Lazy(false)
|
||||
public class SpringContextUtil implements ApplicationContextAware {
|
||||
|
||||
private static ApplicationContext applicationContext;
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
SpringContextUtil.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
public static ApplicationContext getApplicationContext() {
|
||||
return applicationContext;
|
||||
}
|
||||
|
||||
public static Object getBean(String name) {
|
||||
return getApplicationContext().getBean(name);
|
||||
}
|
||||
|
||||
public static <T> T getBean(Class<T> clazz) {
|
||||
return getApplicationContext().getBean(clazz);
|
||||
}
|
||||
|
||||
public static <T> T getBean(String name, Class<T> clazz) {
|
||||
return getApplicationContext().getBean(name, clazz);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 主动向Spring容器中注册bean
|
||||
*
|
||||
* @param name BeanName
|
||||
* @param clazz 注册的bean的类性
|
||||
* @param args 构造方法的必要参数,顺序和类型要求和clazz中定义的一致
|
||||
* @param <T> 泛型
|
||||
* @return 返回注册到容器中的bean对象
|
||||
*/
|
||||
public static <T> T registerBean(String name, Class<T> clazz, Object... args) {
|
||||
return registerBean((ConfigurableApplicationContext) applicationContext, name, clazz, args);
|
||||
}
|
||||
|
||||
/**
|
||||
* 主动向Spring容器中注册bean
|
||||
*
|
||||
* @param applicationContext Spring容器
|
||||
* @param name BeanName
|
||||
* @param clazz 注册的bean的类性
|
||||
* @param args 构造方法的必要参数,顺序和类型要求和clazz中定义的一致
|
||||
* @param <T> 泛型
|
||||
* @return 返回注册到容器中的bean对象
|
||||
*/
|
||||
public static <T> T registerBean(ConfigurableApplicationContext applicationContext, String name, Class<T> clazz,
|
||||
Object... args) {
|
||||
if (applicationContext.containsBean(name)) {
|
||||
Object bean = applicationContext.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) applicationContext.getBeanFactory();
|
||||
beanFactory.registerBeanDefinition(name, beanDefinition);
|
||||
return applicationContext.getBean(name, clazz);
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.hxuanyu.monitor.utils;
|
||||
package com.hxuanyu.common.util;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
@ -12,6 +12,8 @@ import java.util.*;
|
||||
* @version 1.0
|
||||
*/
|
||||
public class BeanUtils {
|
||||
|
||||
|
||||
public static Field findField(Class<?> clazz, String name) {
|
||||
try {
|
||||
return clazz.getField(name);
|
@ -0,0 +1 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.hxuanyu.common.config.CommonConfiguration
|
@ -1,6 +1,6 @@
|
||||
package com.hxuanyu.monitor.config;
|
||||
|
||||
import com.hxuanyu.monitor.utils.BeanUtils;
|
||||
import com.hxuanyu.common.util.BeanUtils;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.scheduling.SchedulingException;
|
||||
import org.springframework.scheduling.TaskScheduler;
|
||||
|
Loading…
x
Reference in New Issue
Block a user