Defining the same Spring bean twice with same name Defining the same Spring bean twice with same name spring spring

Defining the same Spring bean twice with same name


It's valid, but you'll find that one bean is overridden by the other. You'll see this in the logs as

Overriding bean definition for...

This behaviour allows you to override previously supplied bean definitions. It affects the static assembly of your app, and doesn't relate to threading/clustering as suggested in your question.

Note that the DefaultListableBeanFactory allows you to configure this behaviour via setAllowBeanDefinitionOverriding()


This is valid and useful especially when you try to change the implementation of a third party bean (I mean, where you are not allowed to change the implementation of a bean) and Where you need to provide/configure some extra (merge) properties for the bean.

The overriding of the bean depends upon the order of the xmls you provide to build the ApplicationContext through web.xml or stand-alone. The latest bean definition will win the game.


From Spring Boot version 2.1 it is disabled by default. (link)

Bean Overriding: Bean overriding has been disabled by default to prevent a bean being accidentally overridden. If you are relying on overriding, you will need to set spring.main.allow-bean-definition-overriding to true.