package com.example.demo3.controller;
import cn.hutool.core.util.ClassLoaderUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.interfacedemo.service.TestService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.File;
/**
* Date:2022/7/15
*
* @author:zrlie
*/
@RestController
@RequestMapping("demo")
public class DemoController {
@GetMapping(value = "loadClass")
public String loadClass() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
String pluginClass="com.impldemo.service.impl.TestServiceImpl";
ClassLoader classLoader = ClassLoaderUtil.getJarClassLoader(new File("F:\\project\\demo\\implDemo\\target\\implDemo-1.0-SNAPSHOT.jar"));
Class<?> clazz = classLoader.loadClass(pluginClass);
SpringUtil.registerBean("testServiceImpl",clazz.newInstance());
TestService testService = (TestService)SpringUtil.getBean("testServiceImpl");
return testService.testDemo();
}
}
SPRINGBOOT 动态加载JAR包中的Bean到容器
最新推荐文章于 2024-05-11 17:44:33 发布