How to override a Spring @Autowire annotation and set a field to null? -
i spring neophyte working on large spring-based project has extensive coupling between spring beans. trying write integration tests exercise subsets of total application functionality. so, i'd override of autowiring. example, suppose have class
public class mydataserviceimpl implements mydataservice { @qualifier("notneededformydataservicetest") @autowired private notneededformydataservicetest notneededformydataservicetest; //... }
and context file with:
<bean id="mydataservice" class="mydataserviceimpl"> </bean>
in test, have no need use notneededformydataservicetest
field. there way can override @autowired
annotation , set notneededformydataservicetest
null, perhaps in xml file? don't want modify of java classes, want avoid (problematic) configuration of notneededformydataservicetest
.
i tried doing:
<bean id="mydataservice" class="mydataserviceimpl"> <property name="notneededformydataservicetest"><null/></property> </bean>
that doesn't work. intellij informs me "cannot resolve property 'notneededformydataservicetest'", apparently because there no getters , setters field.
i'm using spring framework 3.1.3.
the following configuration should work, took liberty of mixing in java configuration
@configuration //this load beans whichever xml file using @importresource("classpath:/path/beans.xml") public class testconfigloader{ // declare unused bean , inject mydataserviceimpl null. public @bean(name="notneededformydataservicetest") notneededformydataservicetest getnotneededformydataservicetest(){ return null; } ... other configuration beans if required. }
and annotate test class so:
// in test class applicationcontext loaded testconfigloader @contextconfiguration(classes = {testconfigloader.class}) public class mytest { // class body... }
Comments
Post a Comment