简体   繁体   中英

Loading external jars in spring boot

How do we load additional jar at runtime along with boot jar.

Primary jar : Main .jar

Additional jar : Support .jar

Main project is a gradle boot project.

Support project is NOT a gradle project but is given compile time dependencies to the required jars.

Contents of Support project:

@RestController
@RequestMapping("/test")
public class CustomService implements WebMvcConfigurer {


    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public @ResponseBody String get() {
        return "Done!!";
    }
}

What i tried:

java -cp Support.jar:Main.jar -Dloader.path=Support.jar -Xbootclasspath/p:alpn-boot-8.1.11.v20170118.jar -Dloader.main=com.abc.app.MyApplication  org.springframework.boot.loader.PropertiesLauncher

The boot starts up fine but the endpoint is not registered.

NOTE: I had mentioned annotation scanning.

@SpringBootApplication
@ComponentScan("com.abc")
public class MyApplication {
   ....
}

Also the Main.jar will be run from various places by various users. Each user might provide his own version of Support.jar. So, hardcoding the dependency into the gradle file of Main project is not feasible.

Try adding @ComponentScan(basePackages=full.name.of.customservice.package ) to your spring application configuration, or make CustomService the same package as your @SpringApplication class

try using this - org.xeustechnologies.jcl.JarClassLoader from https://github.com/kamranzafar/JCL

JCL is a configurable, dynamic and extensible custom classloader that loads java classes directly from Jar files and other sources.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM