Spring: Bean Shadow

Learn
Aug 29, 2024

--

I had created a bean and had tied it to a service. But when the service references the bean, the bean complains about attribute values that I had not even set on the bean. Why was this happening?

It was happening because the libraries that I was using had beans with the same name, and that bean was overshadowing my bean. Solution, give my bean a unique non-colliding name and reference that unique name from the service using the @Qualifier annotation.

@Bean
public MyBean myBeanWithUniqueName ()

And this gets referenced from the service as below

@Qualifier("myBeanWithUniqueName")
private MyBean myBeanInstance;

The qualifier does the magic of identifying the bean that I am interested in. Now, I no longer get complains about some bean hidden in the library, instead I get complains about my own bean.

--

--

Learn
Learn

Written by Learn

On a continuing learning journey..

No responses yet