Random Creation Of Spring Bean

Viewed 98

Defining bean in a spring-boot application (v 2.3.12) as below:

@MyConditionalProperty(key="name", value = "false")
    @Bean
    @Profile({"local", "dev"})
    public FilterRegistrationBean<Filter> myfilterBean(FilterOne filter){
        return registerFilter(filter, Integer.MIN_VALUE);
    }

    @MyConditionalProperty(key="name", value = "true")
    @Bean
    public FilterRegistrationBean<Filter> myfilterBean(FilterTwo filter){
        return registerFilter(filter, Integer.MIN_VALUE);
    }

    private FilterRegistrationBean<Filter> registerFilter(Filter filter, int order) {
        FilterRegistrationBean<Filter> filterRegistrationBean = new FilterRegistrationBean<>();
        filterRegistrationBean.setFilter(filter);
        filterRegistrationBean.setOrder(order);
        filterRegistrationBean.addUrlPatterns("/v1/*");

        return filterRegistrationBean;
    }

On local active profile, when the value of the "name" property is "false", myfilterBean is created as expected with dependency on FilterOne. Although, across server restarts, don't see it being created consistently.

What could be the reason for this inconsistency and random behavior? Can it be related to the same method name myfilterBean being used to create the bean with one or the other dependency?

1 Answers

Renaming one of the myfilterBean method works. Renamed it as myFilterBeanOne and appropriate bean is loaded based on the conditional property value.

Related