com.jl.springbean.util.JSpringBean Maven / Gradle / Ivy
The newest version!
package com.jl.springbean.util;
import com.jl.JReflect;
import com.jl.JTuple;
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.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* spring bean工具
* 修改bean只需要获取到bean对象再set修改属性即可
*/
@Component
public class JSpringBean implements ApplicationContextAware {
private static ConfigurableApplicationContext configurableApplicationContext;
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
JSpringBean.applicationContext = applicationContext;
configurableApplicationContext = (ConfigurableApplicationContext) applicationContext;
}
/**
* 获取bean
*/
public static T getBean(Class clazz) {
return applicationContext.getBean(clazz);
}
/**
* 获取bean
*/
public static Object getBean(String beanName) {
return applicationContext.getBean(beanName);
}
/**
* 获取bean
*/
public static T getBean(String beanName, Class clazz) {
return (T) applicationContext.getBean(beanName);
}
/**
* 注册bean
*
* @param clazz bean类
* @param args 参数map
* @param
*/
public static T registerBean(Class clazz, Map args) {
String name = getName(clazz);
return registerBean(name, clazz, args);
}
/**
* 注册bean
*
* @param name beanName
* @param clazz bean类
* @param args 参数map
* @param
*/
public static T registerBean(String name, Class clazz, Map args) {
if (existBean(name)) {
throw new RuntimeException("bean重复:" + name);
}
BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(clazz);
Set keys = args.keySet();
Iterator iterator = keys.iterator();
while (iterator.hasNext()) {
String key = iterator.next();
beanDefinitionBuilder.addPropertyValue(key, args.get(key));
}
BeanDefinition beanDefinition = beanDefinitionBuilder.getRawBeanDefinition();
BeanDefinitionRegistry beanFactory = (BeanDefinitionRegistry) configurableApplicationContext.getBeanFactory();
beanFactory.registerBeanDefinition(name, beanDefinition);
return configurableApplicationContext.getBean(name, clazz);
}
/**
* 注册bean
*
* @param obj bean对象
* @param
*/
public static T registerBean(T obj) {
String name = getName(obj.getClass());
return registerBean(name, obj);
}
/**
* 注册bean
*
* @param name beanName
* @param obj bean对象
* @param
*/
public static T registerBean(String name, T obj) {
Class clazz = (Class) obj.getClass();
if (existBean(name)) {
throw new RuntimeException("bean重复:" + name);
}
BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(clazz);
List>> propertys = JReflect.PropertyReflect.getProperty(obj);
for (JTuple.Tuple3> tuple3 : propertys) {
String property = tuple3.getV1();
Object value = tuple3.getV2();
if (value != null) {
beanDefinitionBuilder.addPropertyValue(property, value);
}
}
BeanDefinition beanDefinition = beanDefinitionBuilder.getRawBeanDefinition();
BeanDefinitionRegistry beanFactory = (BeanDefinitionRegistry) configurableApplicationContext.getBeanFactory();
beanFactory.registerBeanDefinition(name, beanDefinition);
return configurableApplicationContext.getBean(name, clazz);
}
/**
* 删除bean
*
* @param name beanName
*/
public static void removeBean(String name) {
if (existBean(name)) {
DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) configurableApplicationContext.getAutowireCapableBeanFactory();
defaultListableBeanFactory.removeBeanDefinition(name);
}
}
/**
* 是否存在bean
*
* @param name beanName
* @return
*/
public static boolean existBean(String name) {
return configurableApplicationContext.containsBean(name);
}
/**
* 获取beanName
*/
private static String getName(Class> clazz) {
String result;
String name = clazz.getName();
result = name.substring(name.lastIndexOf(".") + 1);
//首字母转小写
result = result.substring(0, 1).toLowerCase() + result.substring(1);
return result;
}
}