Error “Overriding bean of same name declared in: class path resource” when Integrating Spring Cloud Sleuth and ActiveMQ Library
Had this weird error when integrating ActiveMQ JMS and Spring Cloud Sleuth,
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.jms.config.DefaultJmsListenerContainerFactory]: Factory method 'jmsListenerContainerFactory' threw exception; nested exception is java.lang.IllegalStateException: @Bean method JMSReceiver.receiverActiveMQConnectionFactory called as bean reference for type [org.apache.activemq.ActiveMQConnectionFactory] but overridden by non-compatible bean instance of type [org.springframework.cloud.sleuth.instrument.messaging.LazyConnectionFactory]. Overriding bean of same name declared in: class path resource [id/co/edw/xxx/api/notificationengineservice/jmsClient/JMSReceiver.class] at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:622) ... 27 common frames omitted Caused by: java.lang.IllegalStateException: @Bean method JMSReceiver.receiverActiveMQConnectionFactory called as bean reference for type [org.apache.activemq.ActiveMQConnectionFactory] but overridden by non-compatible bean instance of type [org.springframework.cloud.sleuth.instrument.messaging.LazyConnectionFactory]. Overriding bean of same name declared in: class path resource [id/co/edw/xxx/api/notificationengineservice/jmsClient/JMSReceiver.class] at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.resolveBeanReference(ConfigurationClassEnhancer.java:418) at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:366) at id.co.edw.xxx.api.notificationengineservice.jmsClient.JMSReceiver$$EnhancerBySpringCGLIB$$10f22401.receiverActiveMQConnectionFactory(<generated>) at id.co.edw.xxx.api.notificationengineservice.jmsClient.JMSReceiver.jmsListenerContainerFactory(JMSReceiver.java:40) at id.co.edw.xxx.api.notificationengineservice.jmsClient.JMSReceiver$$EnhancerBySpringCGLIB$$10f22401.CGLIB$jmsListenerContainerFactory$1(<generated>) at id.co.edw.xxx.api.notificationengineservice.jmsClient.JMSReceiver$$EnhancerBySpringCGLIB$$10f22401$$FastClassBySpringCGLIB$$36560b20.invoke(<generated>) at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363) at id.co.edw.xxx.api.notificationengineservice.jmsClient.JMSReceiver$$EnhancerBySpringCGLIB$$10f22401.jmsListenerContainerFactory(<generated>) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:567) at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ... 28 common frames omitted
It happens because Spring Sleuth overriding bean provided by JMSReceiver,
package id.co.edw.xxx.api.notificationengineservice.jmsClient; import org.apache.activemq.ActiveMQConnectionFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.jms.annotation.EnableJms; import org.springframework.jms.config.DefaultJmsListenerContainerFactory; import java.util.ArrayList; import java.util.Arrays; @Configuration @EnableJms public class JMSReceiver { @Value("somevalue") private String brokerUrl; @Bean @Primary public ActiveMQConnectionFactory receiverActiveMQConnectionFactory() { ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(); activeMQConnectionFactory.setBrokerURL(brokerUrl); activeMQConnectionFactory.setTrustedPackages(new ArrayList<>(Arrays.asList("id.co.edw.xxx.api,java.lang".split(",")))); return activeMQConnectionFactory; } @Bean public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() { DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory(); factory .setConnectionFactory(receiverActiveMQConnectionFactory()); factory.setConcurrency("2"); return factory; } }
Problem solved after remarking Sleuth import on pom.xml,
<dependency> <groupId>javax.activation</groupId> <artifactId>javax.activation-api</artifactId> </dependency> <!-- <dependency>--> <!-- <groupId>org.springframework.cloud</groupId>--> <!-- <artifactId>spring-cloud-starter-sleuth</artifactId>--> <!-- <version>2.1.2.RELEASE</version>--> <!-- </dependency>--> <dependency> <groupId>com.github.tomakehurst</groupId> <artifactId>wiremock-standalone</artifactId> <version>2.19.0</version> <scope>test</scope> </dependency> <!-- swaggwer --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <!-- SMTP SPRING --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> <dependency> <groupId>javax.activation</groupId> <artifactId>javax.activation-api</artifactId> </dependency>
No Comments