I am developing a bunch of Java/Sprint Boot webservices that will all have several identical (ideally, reusable) Spring-based resources:
- many annotation-based Spring Security configurations
- several
@Service
s and@Component
s - many annotation-based event handlers
- annotation-based exception handler
@ControllerAdvice
thatextends ResponseEntityExceptionHandler
- annotation-based configuration files (
@Configuration
)
Ideally I could place these in a library (shared JAR file) and reuse them across the different webservice projects. However I don't believe Spring Boot will scan the entire dependency graph of libraries and load them based on their annotations.
Does anybody know of a way to encourage Spring Boot to do this? For example if I package the following class into a reusable/shareable library JAR:
@ControllerAdvice
@Slf4j
public class ApiExceptionHandler extends ResponseEntityExceptionHandler implements ApiContractConstants {
// ... common exception handling code to be used by
// all services
}
And then pull that in to a Spring Boot webservice (via Maven/Gradle) as a runtime dependency, how do I get Spring Boot to scan, find and load that exception handler for me?
CodePudding user response:
The description makes me think of @SpringBootApplication. The property scanBasePackages of @SpringBootApplication defines base packages to scan for annotated components.
@SpringBootApplication(scanBasePackages = {"org.example"})