Combine Annotation and xml-config?
In this small example the setter injection works well, with the xml config below (bean2 is pointing to bean1)Code:
public class Bean2 { protected Bean1 bean1; public void setBean1(Bean1 b1) { this.bean1 = b1; }
}
Code:
lt;beansgt; lt;bean id=quot;myBean1quot; class=quot;Bean1quot; /gt;
lt;bean id=quot;myBean2quot; class=quot;Bean2quot; p:bean1-ref=quot;myBean1quot;gt; lt;/beangt;
lt;/beansgt;
As soon as I migrate this Bean2 class into a unittest Class (by adding the Annotations), the injection of the bean1 property is not performed any more.Code:
@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners( { DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class, TransactionalTestExecutionListener.class })
@ContextConfiguration(locations = { quot;/applicationcontext1.xmlquot; })
public class Bean2 {
protected Bean1 bean1;
public void setBean1(Bean1 b1) { this.bean1 = b1; } @Test public void assertBean1() { assertNotNull(quot;bean1 missingquot;, this.bean1); }
}
I am wondering, and somehow lost. Am I not allowed to mix xml-config property-references with annotated Test classes?
Please help me understanding this .... what is the reason for this?
( If i remove the p:...-ref in the xml-config and use @Autowire in the Bean2 class it works well again. )
Thank you
Now i found a solution. As I understand the Specification the @Resource or @Autowired annoation is obligatory in this case (SpringJUnit4ClassRunner). Instead of xml wiring p:bean1-ref=quot;myBean1quot;
i may use annoations, and the example runs well then :-) |