how to load JPA Entities and Repositories in sprint boot app from external folder/jar

55 Views Asked by At

I would like to load/aware JPA Entities/repositories from external folder in my springboot application as my app supports dynamic generation of Entities and repositories at runtime based on customer configuraiton. Can someone help how to achieve loadig JPA Entities/repositories from external folder ?

PS : anyone from spring community can help please ?

2

There are 2 best solutions below

1
Ch Vamsi On

JAR typically defined as package file format to aggregate classes and other resources into one file to distribute aplication software/libraries/utilities on the java platform.

command line to run jar file: java -jar filename.jar
5
serod On

You should manually configure application context.

@EntityScan annotation is used to tell Spring where to find entities.

@ComponentScan is used to create beans, as you know @Repository is bean.

@EnableJpaRepositories is used to enable JPA repositories.

So code would look something like this, you should specify package:

@ComponentScan({"com.example", "com.external"})
@EnableJpaRepositories({"com.example", "com.external"})
@EntityScan({"com.example", "com.external"})
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(AppService.class, args);
    }
}

Update

If you want to load jar file, you should install it to your local maven repository using mvn install:install-file -Dfile=<path-to-file>. Then you can add it to your projects pom.xml as dependency.