当前位置: 首页 > 知识库问答 >
问题:

从Spring Boot应用程序中排除@配置

袁霍英
2023-03-14

我有一堆模块(比如3)。两个是基于Spring Boot的模块,另一个是基于Spring的模块。例如模块1-SpringBoot模块2-Spring Boot模块3-Common模块仅基于Spring

模块3@配置文件定义,只需要由模块2选择,而不是模块1。

@SpringBootApplication
@ComponentScan(basePackages = {"com.adobe"}
, excludeFilters = {
    @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = {WorkerConfig.class, WorkerExecutors.class, Worker.class})})
public class Application {

  private static final Logger logger = LoggerFactory.getLogger(Application.class);

  public static void main(String[] args) throws Exception {
    SpringApplication.run(Application.class, args);
  }

}
@Configuration
public class WorkerConfig {

  @Bean
  public WorkerExecutors workerExec() {
    WorkerExecutors executors = new WorkerExecutors();
    return executors;
  }

}
 * <p>Note that the {@code <context:component-scan>} element has an
 * {@code annotation-config} attribute; however, this annotation does not. This is because
 * in almost all cases when using {@code @ComponentScan}, default annotation config
 * processing (e.g. processing {@code @Autowired} and friends) is assumed. Furthermore,
 * when using {@link AnnotationConfigApplicationContext}, annotation config processors are
 * always registered, meaning that any attempt to disable them at the
 * {@code @ComponentScan} level would be ignored.
@SpringBootApplication(exclude= {WorkerExecutors.class, Worker.class,WorkerConfig.class})
public class Application {
java.lang.IllegalStateException: The following classes could not be excluded because they are not auto-configuration classes:
    - com.adobe.repository.worker.lib.config.WorkerConfig
    - com.adobe.acp.repository.worker.lib.core.WorkerExecutors
    - com.adobe.acp.repository.worker.lib.core.Worker

除了放入一个不同的包之外,我知道如何在Spring Boot模块中禁用组件扫描非Spring Boot模块。我不想放不同的包裹。

任何帮助都很感激!!

共有1个答案

宗政子辰
2023-03-14

你可以试试这个对我有用:

@SpringBootApplication
@ComponentScan(excludeFilters  = {@ComponentScan.Filter(
              type = FilterType.ASSIGNABLE_TYPE, classes = {WorkerConfig.class, WorkerExecutors.class, Worker.class})})
 类似资料:
  • 假设我们有一个Spring Boot应用程序和自动配置,其中定义了几个配置 此类通过外部库依赖项提供,并且满足所有条件,因此这些配置中的所有bean都正在加载。 然而,我需要排除FirstNestedConfiguration中提供的bean,可以这样做吗? UPD:由于它是一个简单的Spring Boot应用程序,所以它的运行方式是 没有任何显式的@ComponentScan配置

  • 但是,我有一个组件,它注入了EurekaClient来从中获取服务实例 因此,从这个服务开始,我的应用程序无法加载ApplicationContext。 到目前为止我所尝试的 解决这个问题的恰当方法是什么?

  • 我试图在SpringMVC中运行SpringBoot应用程序,在SpringMVCPOM中添加SpringBoot应用程序依赖项,并扫描SpringBoot包,但我面临以下问题

  • 这是我使用SpringBoot的第一天,我试图理解体系结构,因此我开始构建一个hello world应用程序: 在我的pom.xml中,在maven-shade-plugin下,我将mainClass声明如下: 文件目标是src/main/java/com/demo/helloworld.java,该文件中的代码是: 我错过了什么?

  • 我需要在Spring-boot应用程序中使用默认的ObjectMapper作为单个实例。我是否可以简单地在应用程序内部@autowire ObjectMapper(在Spring-boot应用程序中默认创建的实例)而不创建@bean(因为我不想更改ObjectMapper的任何功能) https://docs.spring.io/spring-boot/docs/current/reference

  • 我正在使用Spring引导应用程序连接MongoDB实例。我们已在MongoDB上启用了以下角色的身份验证 角色:[ { role: "dbOwner ",db: "{{ mongo.database_name }}" } 角色:[ { role: "readWrite ",db: "{{ mongo.database_name }}" } 我们使用conf文件提供凭证 数据: mongodb。u