13693261870
2025-07-02 6708810c4de34dfb9513061432d656f91d56ee3a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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());
    }
}