2 beans with the same name, but in different packages; How to auto-increase them?

I have an application that has 2 beans with the same name, but which are in different packages. The Spring application crashes because it cannot decide which bean to execute. Is there a solution for this? beans does not currently implement specific interfaces.

The following is an edited example of an exception:

Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'dataTransferHandler' for bean class [aaaaa.ws.handler.DataTransferHandler] conflicts with existing, non-compatible bean definition of same name and class [bbbbb.ws.handler.DataTransferHandler] 
+6
source share
2 answers

You will have to give your beans different names - if several beans are defined with the same name, then one of them will be overridden by the previously defined one, so in your case there will be only one bean named dataTransferHandler .

You can give these two beans different names so that they can exist, and you can enter them correctly using: @AutoWired @Qualifier("dataTransferHandler") OR @Resource(name="dataTransferHandler")

+13
source

You can specify the primary = "true" attribute to protect the bean that you want to use for auto-provisioning. But bean names must be different. There is no solution for the same bean name.

At run time, when you get an auto-level class, the primary true bean will be preferred for auto-provisioning. Hope this helps you. Greetings.

0
source

Source: https://habr.com/ru/post/917968/


All Articles