spring-boot - 如何在 Spring boot 2.1.0 启动器配置中将 spring.main.allow-bean-definition-overriding 设置为 true

标签 spring-boot spring-boot-configuration

我维护了一个 spring-boot-starter,它自定义返回的错误属性,例如,调用未知端点时。
这是通过覆盖 org.springframework.boot.web.servlet.error.ErrorAttributes bean 来完成的。

2.0.6 一切正常,但 2.1.0 disables bean overriding by default ,使启动器现在失败并显示以下消息。

Invalid bean definition with name 'errorAttributes' defined in class path resource [com/mycompany/springboot/starter/config/ErrorsConfig.class]: Cannot register bean definition [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=com.mycompany.springboot.starter.config.ErrorsConfig; factoryMethodName=errorAttributes; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/mycompany/springboot/starter/config/ErrorsConfig.class]] for bean 'errorAttributes': There is already [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration; factoryMethodName=errorAttributes; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/error/ErrorMvcAutoConfiguration.class]] bound



如文档中所述,将 spring.main.allow-bean-definition-overriding 属性设置为 true 可以解决问题。
我的问题是如何做到这一点 在启动器中 (我不希望我的 starter 的用户必须更改他们的 application.properties 文件,因为某些特定于我的 starter 的东西)?

我尝试使用该文件中定义的该属性对我的 @Configuration 进行 @PropertySource("classpath:/com/mycompany/starter/application.properties") 注释,但它不起作用。

我错过了什么?有什么办法可以让我的配置覆盖那个 bean?

这是配置的(简化)源代码:
@Configuration
@PropertySource("classpath:/com/mycompany/starter/application.properties")
public class ErrorsConfig {
    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

    @Bean
    public ErrorAttributes errorAttributes() {
        return new DefaultErrorAttributes() {
            @SuppressWarnings("unchecked")
            @Override
            public Map<String, Object> getErrorAttributes(WebRequest request, boolean includeStackTrace) {
                Map<String, Object> errorAttributes = super.getErrorAttributes(request, includeStackTrace);
                // CustomeError is a (simplified) bean of the error attributes we should return.
                CustomError err = new CustomError("myErrorCode", (String) errorAttributes.get("error"));
                return OBJECT_MAPPER.convertValue(err, Map.class);
            }
        };
    }
}

和我的资源文件 com/mycompany/starter/application.properties 包含

spring.main.allow-bean-definition-overriding=true

最佳答案

Spring Boot的ErrorAttributes bean 由 ErrorMvcAutoConfiguration 定义.注释为 @ConditionalOnMissingBean所以如果 ErrorAttributes 它将退出bean 已经被定义。作为您的 ErrorsConfig 定义的 bean类试图覆盖引导的 ErrorAttributes bean 而不是让它退缩,你的 ErrorsConfig类必须在引导后处理 ErrorMvcAutoConfiguration类(class)。这意味着您的启动器存在排序问题。

可以使用 @AutoConfigureBefore 控制处理自动配置类的顺序。和 @AutoConfigureAfter .假设 ErrorsConfig本身是一个在 spring.factories 中注册的自动配置类,您可以通过使用 @AutoConfigureBefore(ErrorMvcAutoConfiguration.class) 注释来解决您的问题.有了这个变化 ErrorsConfig将定义其 ErrorAttributes bean 前ErrorMvcAutoConfiguration尝试这样做会导致 Boot 的 ErrorsAttribute 自动配置 bean 退。

关于spring-boot - 如何在 Spring boot 2.1.0 启动器配置中将 spring.main.allow-bean-definition-overriding 设置为 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53450897/

相关文章:

java - 如何删除java中两个单词之间除下划线之外的所有特殊字符?

java - Spring @PropertySources 值不被覆盖

java - 创建依赖于 Spring Boot 的库,该库有自己的 bootstrap.yml

java - Spring boot webjars未生成,Http 406返回

java - Spring Boot Kafka 消费者多种类型崩溃

java - getPrincipal() 方法返回用户名而不是 UserDetails

java - 在 Spring Boot 中配置 JDBC 身份验证

java - 如何检查Spring Boot应用程序在自动配置类中是否有某个注释

spring-boot - 用于启用 SSL 的 Spring Boot Cassandra 配置