新增部分工具
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
@@ -0,0 +1,131 @@
|
||||
package com.hxuanyu.common.util;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 反射工具,操作Bean对象的属性
|
||||
*
|
||||
* @author hanxuanyu
|
||||
* @version 1.0
|
||||
*/
|
||||
public class BeanUtils {
|
||||
|
||||
|
||||
public static Field findField(Class<?> clazz, String name) {
|
||||
try {
|
||||
return clazz.getField(name);
|
||||
} catch (NoSuchFieldException ex) {
|
||||
return findDeclaredField(clazz, name);
|
||||
}
|
||||
}
|
||||
|
||||
public static Field findDeclaredField(Class<?> clazz, String name) {
|
||||
try {
|
||||
return clazz.getDeclaredField(name);
|
||||
} catch (NoSuchFieldException ex) {
|
||||
if (clazz.getSuperclass() != null) {
|
||||
return findDeclaredField(clazz.getSuperclass(), name);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Method findMethod(Class<?> clazz, String methodName, Class<?>... paramTypes) {
|
||||
try {
|
||||
return clazz.getMethod(methodName, paramTypes);
|
||||
} catch (NoSuchMethodException ex) {
|
||||
return findDeclaredMethod(clazz, methodName, paramTypes);
|
||||
}
|
||||
}
|
||||
|
||||
public static Method findDeclaredMethod(Class<?> clazz, String methodName, Class<?>... paramTypes) {
|
||||
try {
|
||||
return clazz.getDeclaredMethod(methodName, paramTypes);
|
||||
} catch (NoSuchMethodException ex) {
|
||||
if (clazz.getSuperclass() != null) {
|
||||
return findDeclaredMethod(clazz.getSuperclass(), methodName, paramTypes);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Object getProperty(Object obj, String name) throws NoSuchFieldException {
|
||||
Object value;
|
||||
Field field = findField(obj.getClass(), name);
|
||||
if (field == null) {
|
||||
throw new NoSuchFieldException("no such field [" + name + "]");
|
||||
}
|
||||
boolean accessible = field.isAccessible();
|
||||
field.setAccessible(true);
|
||||
try {
|
||||
value = field.get(obj);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
field.setAccessible(accessible);
|
||||
return value;
|
||||
}
|
||||
|
||||
public static void setProperty(Object obj, String name, Object value) throws NoSuchFieldException {
|
||||
Field field = findField(obj.getClass(), name);
|
||||
if (field == null) {
|
||||
throw new NoSuchFieldException("no such field [" + name + "]");
|
||||
}
|
||||
boolean accessible = field.isAccessible();
|
||||
field.setAccessible(true);
|
||||
try {
|
||||
field.set(obj, value);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
field.setAccessible(accessible);
|
||||
}
|
||||
|
||||
public static Map<String, Object> obj2Map(Object obj, Map<String, Object> map) {
|
||||
if (map == null) {
|
||||
map = new HashMap<>(0);
|
||||
}
|
||||
if (obj != null) {
|
||||
try {
|
||||
Class<?> clazz = obj.getClass();
|
||||
do {
|
||||
Field[] fields = clazz.getDeclaredFields();
|
||||
for (Field field : fields) {
|
||||
int mod = field.getModifiers();
|
||||
if (Modifier.isStatic(mod)) {
|
||||
continue;
|
||||
}
|
||||
boolean accessible = field.isAccessible();
|
||||
field.setAccessible(true);
|
||||
map.put(field.getName(), field.get(obj));
|
||||
field.setAccessible(accessible);
|
||||
}
|
||||
clazz = clazz.getSuperclass();
|
||||
} while (clazz != null);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得父类集合,包含当前class
|
||||
*
|
||||
* @param clazz 类型泛型
|
||||
* @return 父类集合
|
||||
*/
|
||||
public static List<Class<?>> getSuperclassList(Class<?> clazz) {
|
||||
List<Class<?>> clazzes = new ArrayList<>(3);
|
||||
clazzes.add(clazz);
|
||||
clazz = clazz.getSuperclass();
|
||||
while (clazz != null) {
|
||||
clazzes.add(clazz);
|
||||
clazz = clazz.getSuperclass();
|
||||
}
|
||||
return Collections.unmodifiableList(clazzes);
|
||||
}
|
||||
}
|
@@ -0,0 +1 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.hxuanyu.common.config.CommonConfiguration
|
Reference in New Issue
Block a user