Java动态加载与热部署:实现用户自定义接口上传热部署的全方位指南

185 阅读1分钟

1. 定义接口

// 计算器接口
public interface Calculator {
    int calculate(int a, int b);
    int add(int a, int b);
}

2. 接口实现类

@Service
public class CalculatorImpl implements Calculator {
    @Autowired
    CalculatorCore calculatorCore;

    // 注解方式实现
    @Override
    public int calculate(int a, int b) {
        int c = calculatorCore.add(a, b);
        return c;
    }

    // 反射方式实现
    @Override
    public int add(int a, int b) {
        return new CalculatorCore().add(a, b);
    }
}

3. 热部署-反射方式

/**
 * 热加载Calculator接口的实现 反射方式
 */
public static void hotDeployWithReflect() throws Exception {
    // 创建URLClassLoader加载jar包
    URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{new URL(jarPath)}, Thread.currentThread().getContextClassLoader());
    
    // 加载实现类的Class对象
    Class clazz = urlClassLoader.loadClass("com.nci.cetc15.calculator.impl.CalculatorImpl");
    
    // 实例化对象
    Calculator calculator = (Calculator) clazz.newInstance();
    
    // 调用方法
    int result = calculator.add(1, 2);
    System.out.println(result);
}

4. 热部署-注解方式

/**
 * 加入jar包后 动态注册bean到spring容器,包括bean的依赖
 */
public static void hotDeployWithSpring() throws Exception {
    // 读取jar包中所有类文件
    Set<String> classNameSet = DeployUtils.readJarFile(jarAddress);
    
    // 创建URLClassLoader加载jar包
    URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{new URL(jarPath)}, Thread.currentThread().getContextClassLoader());
    
    for (String className : classNameSet) {
        // 加载类
        Class clazz = urlClassLoader.loadClass(className);
        
        // 判断是否是Spring的Bean类
        if (DeployUtils.isSpringBeanClass(clazz)) {
            // 注册到Spring容器
            BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(clazz);
            defaultListableBeanFactory.registerBeanDefinition(DeployUtils.transformName(className), beanDefinitionBuilder.getBeanDefinition());
        }
    }
}

5. 删除jar时更新Spring容器

/**
 * 删除jar包时 需要在spring容器删除注入
 */
public static void delete() throws Exception {
    Set<String> classNameSet = DeployUtils.readJarFile(jarAddress);
    URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{new URL(jarPath)}, Thread.currentThread().getContextClassLoader());
    for (String className : classNameSet) {
        Class clazz = urlClassLoader.loadClass(className);
        if (DeployUtils.isSpringBeanClass(clazz)) {
            defaultListableBeanFactory.removeBeanDefinition(DeployUtils.transformName(className));
        }
    }
}

6. 测试

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) applicationContext.getAutowireCapableBeanFactory();
while (true) {
    try {
        // 测试热加载反射方式
        hotDeployWithReflect();
        
        // 测试热加载注解方式
        // hotDeployWithSpring();
        
        // 删除操作
        // delete();
    } catch (Exception e) {
        e.printStackTrace();
        Thread.sleep(1000 * 10);
    }
}