简体   繁体   English

让 Spring Boot 加载在运行时依赖项中定义的带注释的资源

[英]Getting Spring Boot to load annotated resources defined inside runtime dependencies

I am developing a bunch of Java/Sprint Boot webservices that will all have several identical (ideally, reusable) Spring-based resources:我正在开发一堆 Java/Sprint Boot web 服务,它们都有几个相同的(理想情况下,可重用的)基于 Spring 的资源:

  • many annotation-based Spring Security configurations许多基于注解的 Spring Security 配置
  • several @Service s and @Component s几个@Service@Component s
  • many annotation-based event handlers许多基于注释的事件处理程序
  • annotation-based exception handler基于注释的异常处理程序
    • @ControllerAdvice that extends ResponseEntityExceptionHandler @ControllerAdvice extends ResponseEntityExceptionHandler
  • annotation-based configuration files ( @Configuration )基于注释的配置文件( @Configuration

Ideally I could place these in a library (shared JAR file) and reuse them across the different webservice projects.理想情况下,我可以将它们放在一个库(共享 JAR 文件)中,并在不同的 Web 服务项目中重用它们。 However I don't believe Spring Boot will scan the entire dependency graph of libraries and load them based on their annotations.但是我不相信 Spring Boot 会扫描整个库的依赖关系图并根据它们的注释加载它们。

Does anybody know of a way to encourage Spring Boot to do this?有人知道鼓励Spring Boot 这样做的方法吗? For example if I package the following class into a reusable/shareable library JAR:例如,如果我将以下类打包到可重用/可共享的库 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?然后将其作为运行时依赖项拉入 Spring Boot Web 服务(通过 Maven/Gradle),如何让 Spring Boot 为我扫描、查找和加载该异常处理程序?

The description makes me think of @SpringBootApplication.描述让我想起了@SpringBootApplication。 The property scanBasePackages of @SpringBootApplication defines base packages to scan for annotated components. @SpringBootApplication 的属性 scanBasePackages 定义基本包以扫描带注释的组件。

@SpringBootApplication(scanBasePackages = {"org.example"})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM