package com.ruoyi.buss.common.dynamic;
|
|
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
import org.springframework.context.ConfigurableApplicationContext;
|
|
public class DynamicBeanRegistrar {
|
private final ConfigurableApplicationContext context;
|
private final DynamicCompiler compiler;
|
|
public DynamicBeanRegistrar(ConfigurableApplicationContext context) {
|
this.context = context;
|
this.compiler = new DynamicCompiler();
|
}
|
|
public void registerBean(String beanName, String className, String sourceCode) throws Exception {
|
DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) context.getBeanFactory();
|
Class<?> clazz = compiler.compile(className, sourceCode);
|
beanFactory.registerSingleton(beanName, clazz.getDeclaredConstructor().newInstance());
|
}
|
}
|