javaspringspring-bootmaintainability

Spring Boot: Bring In Configuration Classes From External Jar File


I'm trying to reduce the amount of "copy/paste" code I have across multiple Spring projects, such as a custom WebSecurityConfigurerAdapter. This adapter is the exact same code across all projects I have so far, so normally in these situations I create a standalone code base and export to a jar to keep things maintainable.

The issue is that it seems that when I do that, Spring no longer notices that class as one that it needs to ingest into it's auto configuration, as when I removed that security class from my project and brought in the jar library now containing it, the security feature I developed was broken. Undid that change and it was working again.

Is there any way that I can tell Spring to look at specific jar files to ingest into it's configuration so that I can keep myself from having classes upon classes of configuration code that's ultimately repeated across multiple projects?


Solution

  • You can use the @ComponentScan annotation with arguments in your application class to point to multiple modules. It is explained here. This will override the default one included in @SpringBootApplication