代码拉取完成,页面将自动刷新
同步操作将从 吴博/动态编译工具 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
1.1.0版本后升级到jdk17 SpringBoot3+
<dependency>
<groupId>com.gitee.wb04307201</groupId>
<artifactId>loader-util</artifactId>
<version>1.1.3</version>
</dependency>
使用DynamicBean需要配置@ComponentScan,包括cn.wubo.loader.util.SpringContextUtils文件
@GetMapping(value = "/test/bean")
public String testBean(){
String javaSourceCode = "package cn.wubo.loader.util;\n" +
"\n" +
"public class TestClass {\n" +
" \n" +
" public String testMethod(String name){\n" +
" return String.format(\"Hello,%s!\",name);\n" +
" }\n" +
"}";
String fullClassName = "cn.wubo.loader.util.TestClass";
String methodName = "testMethod";
String beanName = DynamicBean.init(DynamicClass.init(javaSourceCode,fullClassName)).load();
return (String) MethodUtils.invokeBean(beanName,methodName,"world");
}
@GetMapping(value = "/test/class")
public String testClass(){
String javaSourceCode = "package cn.wubo.loader.util;\n" +
"\n" +
"public class TestClass {\n" +
" \n" +
" public String testMethod(String name){\n" +
" return String.format(\"Hello,%s!\",name);\n" +
" }\n" +
"}";
String fullClassName = "cn.wubo.loader.util.TestClass";
String methodName = "testMethod";
DynamicClass dynamicClass = DynamicClass.init(javaSourceCode, fullClassName).compiler();
Class<?> clasz = dynamicClass.load();
return (String) MethodUtils.invokeClass(clasz, methodName, "world");
}
@GetMapping(value = "/test/jar")
public String testJar(){
Class<?> clasz = DynamicJar.init("D:\\maven-repository\\repository\\cn\\hutool\\hutool-all\\5.3.2\\hutool-all-5.3.2.jar").load("cn.hutool.core.util.IdUtil");
return (String) MethodUtils.invokeClass(clasz, "randomUUID");
}
@GetMapping(value = "/loadAndInvokeGroovy")
public String loadAndInvokeGroovy() {
String javaSourceCode = "package cn.wubo.loader.util;\n" +
"\n" +
"public class TestClass {\n" +
" \n" +
" public String testMethod(String name){\n" +
" return String.format(\"Hello,%s!\",name);\n" +
" }\n" +
"}";
String methodName = "testMethod";
Class<?> clasz = DynamicGroovy.init(javaSourceCode).load();
return (String) MethodUtils.invokeClass(clasz, methodName, "world");
}
@GetMapping(value = "/loadController")
public String loadController() {
String fullClassName = "cn.wubo.loaderutiltest.DemoController";
String javaSourceCode = """
package cn.wubo.loaderutiltest;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "test")
public class DemoController {
@GetMapping(value = "hello")
public User hello(@RequestParam(value = "name") String name) {
return new User(name);
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public static class User {
private String name;
}
}
""";
return DynamicController.init(DynamicClass.init(javaSourceCode, fullClassName)).load();
}
GET http://localhost:8080/test/hello?name=world
Accept: application/json
Hello,world!
@GetMapping(value = "/testAspect")
public String testAspect() throws InstantiationException, IllegalAccessException {
String javaSourceCode = "package cn.wubo.loader.util;\n" +
"\n" +
"public class TestClass {\n" +
" \n" +
" public String testMethod(String name){\n" +
" return String.format(\"Hello,%s!\",name);\n" +
" }\n" +
"}";
String fullClassName = "cn.wubo.loader.util.TestClass";
String methodName = "testMethod";
DynamicClass dynamicClass = DynamicClass.init(javaSourceCode, fullClassName).compiler();
Class<?> clasz = dynamicClass.load();
Object obj = MethodUtils.proxy(clasz.newInstance());
return (String) MethodUtils.invokeClass(obj, methodName, "world");
}
输出示例
2023-04-08 21:22:14.174 INFO 32660 --- [nio-8080-exec-1] cn.wubo.loader.util.aspect.SimpleAspect : SimpleAspect before cn.wubo.loader.util.TestClass testMethod
2023-04-08 21:22:14.175 INFO 32660 --- [nio-8080-exec-1] cn.wubo.loader.util.aspect.SimpleAspect : SimpleAspect after cn.wubo.loader.util.TestClass testMethod
2023-04-08 21:22:14.175 INFO 32660 --- [nio-8080-exec-1] cn.wubo.loader.util.aspect.SimpleAspect : StopWatch 'cn.wubo.loader.util.TestClass testMethod': running time = 65800 ns
可以通过继承IAspect接口实现自定义切面,并通过MethodUtils.proxy(Class<?> clazz, Class<? extends IAspect> aspectClass)方法调用切面
因为本地和服务器的差异导致classpath路径不同,
进而使服务上动态编译class时会发生找不到import类的异常,
因此需要对maven编译配置和启动命令做出一定的修改
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<!-- 是否要把第三方jar加入到类构建路径 -->
<addClasspath>true</addClasspath>
<!-- 外部依赖jar包的最终位置 -->
<classpathPrefix>lib/</classpathPrefix>
<!--指定jar程序入口-->
<mainClass>cn.wubo.loaderutiltest.LoaderUtilTestApplication</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<!-- lib依赖包输出目录,打包的时候不打进jar包里 -->
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<excludeTransitive>false</excludeTransitive>
<stripVersion>false</stripVersion>
<includeScope>runtime</includeScope>
</configuration>
</execution>
</executions>
</plugin>
-Dloader.path=lib/
java -jar -Dloader.path=lib/ loader-util-test-0.0.1-SNAPSHOT.jar
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。