简体   繁体   English

如何使用条件注释覆盖 spring 中的配置(例如)

[英]How can I override configuration in spring using conditional annotations (for example)

I have the following class available in a dependency jar:我在依赖 jar 中有以下可用类:


@Configuration
@EnableSpringDataWebSupport
public class RepositoryRestConfig extends RepositoryRestConfigurerAdapter {

    @Override
    public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
        config.setBasePath(CoreHttpPathStore.REST_PATH);
        config.setReturnBodyOnCreate(true);
        config.setReturnBodyOnUpdate(true);
        config.hasResourceMappingForDomainType(GrantedAuthority.class);
    }

    @Override
    public void configureJacksonObjectMapper(ObjectMapper mapper) {
        super.configureJacksonObjectMapper(mapper);
        mapper.registerModule(new JavaTimeModule());
        mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
    }

}

I want this to be the default configuration, but in some case, I must add some configuration, for this, I create in my application this new class:我希望这是默认配置,但在某些情况下,我必须添加一些配置,为此,我在我的应用程序中创建了这个新类:


@Configuration
@EnableSpringDataWebSupport
public class MyAppRepositoryRestConfig extends RepositoryRestConfig {

    @Override
    public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
        super.configureRepositoryRestConfiguration(config);
        config.exposeIdsFor(
                User.class,
                Client.class,
                Role.class,
                Organization.class,
        );
    }

//  @Override
//  public void configureJacksonObjectMapper(ObjectMapper mapper) {
//      super.configureJacksonObjectMapper(mapper);
//  }

}

The problem is that the method configureRepositoryRestConfiguration in the jar is called twice, which makes me believe this is not what I should do.问题是jar中的configureRepositoryRestConfiguration方法被调用了两次,这让我觉得这不是我应该做的。

How can I boot my configuration conditionally ?如何有条件地启动我的配置?

You can try to do this, create your config as a bean:您可以尝试这样做,将您的配置创建为 bean:

@Bean
public RepositoryRestConfigurer repositoryRestConfigurer() {
    return new RepositoryRestConfigurer() {
        @Override
        public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
            config.exposeIdsFor(Class1.class, Class2.class);
        }
    };
}

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

相关问题 如何以编程方式配置 Spring 的 @Configuration 注释? - How can I programmatically configure Spring's @Configuration annotations? 如何使用Spring @Configuration覆盖导入的资源? - How to override an imported resource using Spring @Configuration? 如何在Spring中使用注释应用方面? - How can I apply an aspect using annotations in Spring? 如何在Spring 4中使用注释重新加载属性文件? - How can I reload properties file in Spring 4 using annotations? 如何使用Spring和注解加载策略图? - How can i load the strategy map using spring and annotations? 如何静态检测丢失的@Override注释? - How can I statically detect missing @Override annotations? 如何使用Hibernate / JPA注释覆盖GenerationType策略? - How do I override the GenerationType strategy using Hibernate/JPA annotations? Spring启动时使用Kafka Testcontainer如何动态覆盖Bootstrap服务器的配置? - How Do I Override the Bootstrap Servers' Configuration Dynamically when Using Kafka Testcontainers in Spring Boot? 如何使用 Swagger 注释在 Swagger 中设置描述和示例? - How can I set a description and an example in Swagger with Swagger annotations? 我可以使用 Spring 作为 CDI 混合 JEE 和 Spring 注释吗? - Can I mix JEE and Spring annotations using Spring as CDI?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM