mardi 3 mars 2015

How to override spring bean definition defined in xml with @Configuration class

I have been trying to figure this out for some time but am not able to get it to work. I am using spring version 3.2.3.RELEASE and I think this may be causing some of my issues. My goal is to override the bean defined in the xml with a configuration file that has imported the configuration.The last class listed is my TestAppConfig where I import the AppConfig and want to simply override the bean implementation with mock implementations. However this is not working for me. Any suggestions would be appreciated.


This is my bean definition class where I define a couple of beans.



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://ift.tt/GArMu6"
xmlns:xsi="http://ift.tt/ra1lAU" xmlns:context="http://ift.tt/GArMu7"
xsi:schemaLocation="http://ift.tt/GArMu6
http://ift.tt/1jdM0fG
http://ift.tt/GArMu7
http://ift.tt/1jdLYo7">

<import resource="rest-common.xml"/>

<context:property-placeholder location="classpath:module.properties"/>


<bean name="vcheckResource" class="com.foo.vcheck.resources.VcheckResource" />

<bean name="vcheckProvider" class="com.foo.vcheck.provider.VcheckProvider" />

<bean name="Vcheck" class="com.foo.vcheck.provider.VcheckMessageRouter" />

</beans>


Here is the production @Configuration class where I import the bean configuration file.



@Configuration
@Import(RestAppConfig.class)
@ImportResource({VcheckAppConfig.ModuleResources})
public class VcheckAppConfig {

public static final String ModuleResources = "classpath:bcpbx-api-vcheck-rest-beans.xml";

}


This is my Testing configuration class where I want to override the implementations with my Mockito mocks and in the unit test the production code should be injected with these mock classes. However the beans in the xml configuration are not being overridden for whatever reason. If I remove the import it will use the beans from this class so I know this is working. Is this possible with 3.x version of spring?



@Configuration
@Import(VcheckAppConfig.class)
public class TestAppConfig {

@Bean
public Account testAccount() {
return new Account("TEST_ACCOUNT", new Vendor("TEST_VENDOR"));
}

@Bean(name ="vcheckResource")
public VcheckResource vcheckResource() {
return new VcheckResource(vcheckProvider(), new UUIDGenerator());
}

@Bean(name="vcheckProvider")
public IVcheckProvider vcheckProvider() {
System.out.println("CALLIGN GET MOCK");
return Mockito.mock(VcheckProvider.class);
}

@Bean
public IMessageRouter messageRouter() {
return Mockito.mock(IMessageRouter.class);
}

@Bean
public ICommandResponseCallbackRegistry responseRegistry() {
return Mockito.mock(ICommandResponseCallbackRegistry.class);
}

}

Aucun commentaire:

Enregistrer un commentaire