Home > Back-end >  Spring Boot load extra Controllers and Services from a jar inside external folder
Spring Boot load extra Controllers and Services from a jar inside external folder

Time:07-26

I have a Spring Boot Application with few Controllers, Services, Entities and Repositories, and I want to load at runtime some extra jars with Controllers, Services ecc. that I'll put in a directory, for example like this:

--MyFolder
     |
     SpringApp.jar
     PluginsFolder
        |
        Plugin1.jar
        Plugin2.jar

So while having SpringApp.jar as a base Spring Boot Application, is it possible to load some extra Controllers and so by simply putting jars in that folder? I don't care if it need to reboot, any help?

CodePudding user response:

For adding controllers and services externally:

@ComponentScan(basePackages = {"com.path.to.class", "com.path.to.class"})

For adding Entity:

@EntityScan(basePackages = {"com.path.to.class", "com.path.to.class"})

For adding Repositories:

@EnableJpaRepositories(basePackages = {"com.path.to.class", "com.path.to.class"})

CodePudding user response:

If you have control over these jars, I'll suggest to use custom spring boot starter in this way the jar autoconfigure itself and you don't need extra annotation.

custom spring boot starter

  • Related