Browse Source

test reload

liuyanming.lym 1 year ago
parent
commit
e7a7d97e3c

+ 4 - 5
plugin-impl/pom.xml

@@ -2,13 +2,9 @@
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
-    <parent>
-        <groupId>com.plugin</groupId>
-        <artifactId>SpringBootPluginTest</artifactId>
-        <version>0.0.1-SNAPSHOT</version>
-    </parent>
     <groupId>com.plugin</groupId>
     <artifactId>plugin-impl</artifactId>
+    <version>0.0.3-SNAPSHOT</version>
     <name>plugin-impl</name>
     <description>插件实现类</description>
 
@@ -16,11 +12,14 @@
         <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-context</artifactId>
+            <version>5.3.10</version>
+            <scope>provided</scope>
         </dependency>
         <dependency>
             <groupId>com.plugin</groupId>
             <artifactId>plugin-interface</artifactId>
             <version>0.0.1-SNAPSHOT</version>
+            <scope>provided</scope>
         </dependency>
     </dependencies>
 

+ 3 - 4
plugin-impl/src/main/java/com/plugin/impl/PluginImpl.java

@@ -1,9 +1,7 @@
 package com.plugin.impl;
 
-import com.plugin.AccessData;
 import com.plugin.CustomAccessInterface;
 import com.plugin.PluginInterface;
-import com.plugin.Result;
 import org.springframework.beans.factory.annotation.Autowired;
 
 public class PluginImpl implements PluginInterface {
@@ -12,7 +10,8 @@ public class PluginImpl implements PluginInterface {
 
     @Override
     public String sayHello(String name) {
-        Result result = customAccessInterface.customAccess(new AccessData());
-        return result.message;
+        return "V3";
+        //Result result = customAccessInterface.customAccess(new AccessData());
+        //return result.message;
     }
 }

+ 1 - 3
plugin-users/src/main/java/com/plugin/SpringBootPluginTestApplication.java

@@ -1,12 +1,10 @@
 package com.plugin;
 
-import com.plugin.users.config.PluginImportBeanDefinitionRegistrar;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
-import org.springframework.context.annotation.Import;
 
 @SpringBootApplication
-@Import(PluginImportBeanDefinitionRegistrar.class)
+//@Import(PluginImportBeanDefinitionRegistrar.class)
 public class SpringBootPluginTestApplication {
     public static void main(String[] args) {
         SpringApplication.run(SpringBootPluginTestApplication.class, args);

+ 4 - 4
plugin-users/src/main/java/com/plugin/users/controller/TestController.java

@@ -55,11 +55,11 @@ public class TestController {
         Class<?> clazz = classLoader.loadClass(pluginClass);
         springUtil.registerBean(clazz.getName(), clazz);
         Object bean = springUtil.getBean(clazz.getName());
-        if(bean instanceof PluginInterface){
-            PluginInterface plugin = (PluginInterface)bean;
+        if (bean instanceof PluginInterface) {
+            PluginInterface plugin = (PluginInterface) bean;
             this.pluginInterface = plugin;
             return plugin.sayHello("test reload");
-        }else{
+        } else {
             log.info(bean.getClass().getInterfaces()[0].getName());
             return bean.toString();
         }
@@ -77,7 +77,7 @@ public class TestController {
         springUtil.removeBean(clazz.getName());
         this.pluginInterface = null;
         Object bean = springUtil.getBean(clazz.getName());
-        if(bean!=null) {
+        if (bean != null) {
             log.info(bean.toString());
         }
         return clazz.getName();

+ 12 - 4
plugin-users/src/main/java/com/plugin/users/utils/ClassLoaderUtil.java

@@ -1,5 +1,6 @@
 package com.plugin.users.utils;
 
+import lombok.SneakyThrows;
 import lombok.extern.slf4j.Slf4j;
 
 import java.lang.reflect.Method;
@@ -18,12 +19,19 @@ import java.net.URLClassLoader;
  */
 @Slf4j
 public class ClassLoaderUtil {
+    private static Method method = Loader_addURL_Method();
+
+    @SneakyThrows
+    private static Method Loader_addURL_Method() {
+        Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
+        if (!method.isAccessible()) {
+            method.setAccessible(true);
+        }
+        return method;
+    }
+
     public static ClassLoader getClassLoader(String url) {
         try {
-            Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
-            if (!method.isAccessible()) {
-                method.setAccessible(true);
-            }
             URLClassLoader classLoader = new URLClassLoader(new URL[]{}, ClassLoaderUtil.class.getClassLoader());
             method.invoke(classLoader, new URL(url));
             return classLoader;

+ 1 - 1
plugin-users/src/main/resources/application.properties

@@ -1,4 +1,4 @@
 spring.main.allow-bean-definition-overriding=true
 
-targetUrl=file:/D:/workspaces/projects/SpringBootPluginTest/plugins/plugin-impl-0.0.1-SNAPSHOT-jar-with-dependencies.jar
+targetUrl=file:/Users/liuyanming/IdeaProjects/intelnal/springs-boot-plugin-test/plugins/plugin-impl-0.0.1-SNAPSHOT-jar-with-dependencies.jar
 pluginClass=com.plugin.impl.PluginImpl

BIN
plugins/plugin-impl-0.0.2-SNAPSHOT.jar


BIN
plugins/plugin-impl-0.0.3-SNAPSHOT.jar


+ 1 - 1
pom.xml

@@ -20,7 +20,7 @@
 
     <modules>
         <module>plugin-users</module>
-        <module>plugin-impl</module>
+<!--        <module>plugin-impl</module>-->
         <module>plugin-interface</module>
     </modules>
 </project>