【问题标题】:Custom Bean not loaded in Spring Boot Cloud AutoConfiguration ClassSpring Boot Cloud AutoConfiguration 类中未加载自定义 Bean
【发布时间】:2021-07-30 18:16:31
【问题描述】:

我想提供一个特定的Bean,让这个Bean覆盖Spring Cloud中的BeanAutoConfiguration 类。

第一次尝试

因此我创建了一个 Configuration 类:

@Configuration
public class MyLocalConfig {
  @Bean
  public ApiClient apiClient() throws IOException {
    return ClientBuilder.standard(false).build();
  }
}

使用@Primary@Order 注释来确定优先级并没有帮助。

第二次尝试(编辑)

我也尝试使用 AutoConfiguration。但即使是@AutoConfigureBefore 注解也会被忽略。

@Configuration
@AutoConfigureBefore(KubernetesClientAutoConfiguration.class)
public class LocalKubeAutoConfiguration {
  @Bean
  public ApiClient apiClient() throws IOException {
    return ClientBuilder.standard(false).build();
  }
}

我的 ConfigurationBeans 总是在 KubernetesClientAutoConfiguration 中的 Beans 之后实例化班级。 因此 AutoConfiguration 类不使用我的 Bean

文档说:At any point, you can start to define your own configuration to replace specific parts of the auto-configuration

问题:

  • 我的错误是什么?
  • 如何确定配置的优先级?

这是我的其他代码:

主类

@SpringBootApplication
public class SpringBootAdminApp {
  public static void main(String[] args) {
    SpringApplication.run(SpringBootAdminApp.class, args);
  }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.5</version>
        <relativePath/>
    </parent>
    <groupId>com.example</groupId>
    <artifactId>testme</artifactId>
    <version>1-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-kubernetes-client-all</artifactId>
            <version>2.0.2</version>
        </dependency>
    </dependencies>
</project>

【问题讨论】:

  • 你的配置是什么包,你@SpringBootApplication注解类的包是什么?
  • 你可以尝试在@SpringBootApplication 类中使用@Import 配置
  • @VladimirShefer SpringBootApplication 类的包和配置是一样的。导入也无济于事。

标签: spring-boot spring-cloud spring-cloud-config


【解决方案1】:

在主类中 添加@SpringBootApplication(scanBasePackages = ....) 所以定制的包裹会被扫描 这个注解只是告诉 spring 在哪里搜索。

https://www.baeldung.com/spring-component-scanning

【讨论】:

    【解决方案2】:

    Spring Boot Cloud 使用Bootstrap Configuration 加载配置。

    默认情况下,引导属性(不是 bootstrap.properties,而是 在引导阶段加载的属性)添加 高优先级,因此它们不能被本地配置覆盖。

    因此我必须添加一个 BootstrapConfiguration:

    org.springframework.cloud.bootstrap.BootstrapConfiguration=\
    com.demo.LocalKubeConfig
    

    BootstrapConfiguration中的Bean会在KubernetesClientAutoConfiguration之前被加载。

    【讨论】:

      猜你喜欢
      • 2020-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-18
      • 1970-01-01
      • 2020-12-22
      • 2016-09-10
      相关资源
      最近更新 更多