Friday, 15 July 2011

java - How to perform Constructor Injection in this scenario? -



java - How to perform Constructor Injection in this scenario? -

suppose have class name a.java. in a.java class have default constructor

a(){ }

and 1 custom constructor have parameter

a(b b){ super(b); }

now class c extending class utilize getter of class. 1 time again c has constructor 1 parameter

c(a a) { this.a = a; }

again have 1 class d , in have constructor

d(c c ) {this.c = c; }

now question how can perform spring construction injection in scenario?

here context wrote:

<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-3.2.xsd"> <bean id="a" class="com.my.springtest.a"> <constructor-arg ref="b"/> </bean> <bean id="b" class="com.my.springtest.b"/> <bean id="c" class="com.my.springtest.c"> <constructor-arg ref="a"/> </bean> <bean id="d" class="com.my.springtest.d"> <constructor-arg ref="c"/> </bean> </beans>

save file , set classpath(eg. src/main/resources if utilize maven). can create context in junit:

@contextconfiguration(locations = { "classpath:/test-context.xml" }) @runwith(springjunit4classrunner.class) public class springtest { @autowired private d d; @test public void testbean() { assert.assertnotnull(d); } }

java spring inversion-of-control constructor-injection

No comments:

Post a Comment