简体   繁体   中英

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

I have the following class available in a dependency 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.

How can I boot my configuration conditionally ?

You can try to do this, create your config as a bean:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM