0

So I need to override a bean provided by a dependency in order to customize some configuration.

Is there any way to allow overriding just for THAT particular name?

I don't want to set

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

That's scary. I just want to override one particular named bean and disallow overriding in all other instances.

** EDIT **

    @Bean
    @Primary
    fun vaadinAuthenticationSuccessHandler(
            httpService: HttpService,
            vaadinRedirectStrategy: VaadinRedirectStrategy
    ): VaadinAuthenticationSuccessHandler {
        return VaadinUrlAuthenticationSuccessHandler(httpService, vaadinRedirectStrategy, "/")
    }

results in

The bean 'vaadinAuthenticationSuccessHandler', defined in class path resource [n/c/s/config/security/VaadinAwareSecurityConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [org/vaadin/spring/security/config/VaadinSharedSecurityConfiguration.class] and overriding is disabled.

It's worth noting that similar code I've seen actually uses

@Bean(name = VaadinSharedSecurityConfiguration.VAADIN_AUTHENTICATION_SUCCESS_HANDLER_BEAN)

(which doesn't make a difference, but it's worth noting all the same)

User1291
  • 7,664
  • 8
  • 51
  • 108

1 Answers1

3

In one of your @Configuration class, you can declare a @Bean with the same class as the one coming from your dependency library and mark it as @Primary to override the bean.

@Configuration
public class MyConfiguration {
    @Bean
    @Primary
    public BeanClassFromDependency mrBean() {
        return new YourOwnImplementationForBeanClassFromDependency();
    }
}

Subsequently, you can autowire as usual.

@Autowired
private BeanClassFromDependency theBeanThatGotAway;
Mr.J4mes
  • 9,168
  • 9
  • 48
  • 90
  • 1
    it doesn't work because you're trying to create a new bean with the exact same name. Give your new bean a unique name like @Bean(name="mrBean") and mark it with @Primary. – Mr.J4mes May 09 '19 at 12:41