1

I have a Spring Boot REST application with JPA entities and Repository classes (and related services) that works very well. Now I would like to reuse these classes for other purposes, like weekly CRON jobs and similar one-time processes which will be run from the command line.

What would be the best way to do this? The challenge is that the persistence context properties are set in application.properties, and the persistence context isn't initialized unless the Application class is initialized.

I can break out all of these classes into a separate project, and use a different way to define the persistence context there, but this becomes more of a maintenance headache if anything changes with the entities or DAO methods.

What I would really like is to have a way, from the command line, to tell Spring Boot to run another class instead of the main Application (and have the persistence context properly initialized). Any way to do this?

(Note I asked a similar question which got no response: Possible to use Spring Boot repositories from another main class?)

[Edit] is it possible to do this by creating a @component that implements the CommandLineRunner? I just want it to run a simple one-time process and not the full REST application.

Community
  • 1
  • 1
Andrew
  • 199
  • 1
  • 14

1 Answers1

3

There are a number of ways you could do this. You can have multiple Main classes, and then select which application yuo want to start select main class, however if you don't know how ComponetScan works you will end up loading both applications if you are not careful.

Another way is to use Profiles, you can set the profile when you start your spring app, and then have your web profile that will start Tomcat, and a command line profile that will not .

In the project I'm working on we have choosen to have the data-layer as a completly separate module (same gradle project), which has it's own Spring Context. The data-layer spring context is then used as the parent context for other applications, as a reusable component. It is a somewhat cleaner separations of concerns, were the shared code is clearly marked, instead of having multiple applications inside the same code mudule.

Community
  • 1
  • 1
Klaus Groenbaek
  • 4,820
  • 2
  • 15
  • 30
  • Thanks for the response. I've tried the multiple Main class thing but either I'm doing it wrong or it doesn't initialize the persistence context properly. I did want to separate out the DAO layer from the REST application but again I was having too much trouble getting the persistence context working correctly. Do you have a simple example how to do this but still have it accessible from the REST controller / service layer? – Andrew Dec 20 '16 at 00:02
  • I was finally able to get this working with Profiles, but it was not as straightforward as you describe. I will write up a more detailed explanation as a separate answer. But I do thank you for pointing me in the right direction, and I am still curious how to have the persistence context as a separate Spring Boot project. – Andrew Jan 06 '17 at 17:15
  • @Klaus When you say a data-layer as a completely separate module, does it have its own Main class? Would you mind, sharing the file structure please? I've a Java 6 project with separate DO and Service module. I'm struggling to do something like that using Spring Boot? – Kay Feb 19 '17 at 05:33