java - Spring 启动: How to include a configuration class that is not in my base package?

标签 java spring spring-boot component-scan

我有一个相当标准的 Spring Boot 应用程序,它是使用几个 gradle 模块的 gradle 构建的。这是目录布局:

 - root
   - serviceA
     - src/main/java
       - org.example.serviceA
         - ServiceAApplication.java
   - serviceB
   - serviceC
   - common
     - src/main/java
       - org.example.common
         - CommonSecurityConfiguration.java

我想要做的是包含 serviceA 中共享common 模块中的CommonSecurityConfiguration 类。请注意,ServiceAApplicationCommonSecurityConfiguration 驻留在不同的基础包中。

我尝试在我的 ServiceAApplication 上使用 @Import(CommonSecurityConfiguration.class),但这根本没有任何可观察到的效果。

唯一有效的方法是像这样注释ServiceAApplication:

@SpringBootApplication(basePackages = { "org.example.serviceA", "org.example.common"})
public class ServiceAApplication { ... }

这种方法有效,但对我来说似乎非常粗粒度 - 它将导入它在 org.example.common 中找到的每个组件和配置。

有更好的方法吗?我可以通过一一列出各个类来将其包含到组件扫描中吗?

最佳答案

尝试使用 @Import(CommonSecurityConfiguration.class) 上面的配置类。所以它看起来像这样:

@Configuration
@Import(CommonSecurityConfiguration.class)
public class ServiceAConfiguration { ... }

关于java - Spring 启动: How to include a configuration class that is not in my base package?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60097674/

相关文章:

java - 使用 @JsonAnyGetter 时忽略 PropertyNamingStrategy

spring - 如何在 Spring Boot 应用程序中设置 Hibernate bulk_id_strategy?

Java + Docker : Different datasource url

java - 如何通过其他变量访问java类中的常量?

java - 如果 spring.beaninfo.ignore 设置为 true,Spring 会做什么?

spring - 您如何在 Spring 容器之外使用 Spring Data JPA?

java - 为什么我不能在java中调用nextLine()方法两次?

java - 在java中为Cplex设置de GAP容差

java - CustomEditorConfigurer 的 customEditors 属性与 RangeArrayPropertyEditor、Spring 4.3.3 与 Spring Batch 3.0.7

带 DLL 的 Spring-boot 可执行 tomcat