Reputation: 2924
I have two Spring projects: a client and server application. I would like to create a third integration test project. However my tests fail with:
Caused by: org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'meterRegistry' defined in class path resource [applicationContext.xml]: Cannot register bean definition [Generic bean: class [io.micrometer.core.instrument.logging.LoggingMeterRegistry]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [applicationContext.xml]] for bean 'meterRegistry': There is already [Generic bean: class [io.micrometer.core.instrument.logging.LoggingMeterRegistry]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [applicationContext.xml]] bound.
Each of the applications defines their own (identical) meterRegistry bean in their respective applicationContext.xml files.
Is there any way of telling Spring that both applications can share the meterRegistry bean?
Upvotes: 2
Views: 362
Reputation: 2924
I would up adding an application.properties
file to the integration test project that contained:
spring.main.allow-bean-definition-overriding=true
Upvotes: 0
Reputation: 16469
You have to use <import>
field in the applicationContext.xml
:
How to use ?
For example :
applicationContextClient.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id = "example" class="com.test.Example"/>
</beans>
applicationContextServer.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="applicationContextClient.xml" />
<bean id="Example2" class="com.test.Example2">
<property name="example" ref="example"/>
</bean>
</beans>
Upvotes: 1