Skip to content

Commit

Permalink
evolve(demo1):dependency injection - setter injection
Browse files Browse the repository at this point in the history
  • Loading branch information
chengcyber committed Dec 25, 2017
1 parent a770408 commit 548824f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
7 changes: 7 additions & 0 deletions spring-demo-1/src/applicationContext.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@

</bean>

<bean id="myCricketCoach"
class="com.luv2code.springdemo.CricketCoach">

<!-- set up setter injection -->
<property name="fortuneService" ref="myFortuneService"></property>

</bean>
</beans>


Expand Down
28 changes: 28 additions & 0 deletions spring-demo-1/src/com/luv2code/springdemo/CricketCoach.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.luv2code.springdemo;

public class CricketCoach implements Coach {

private FortuneService fortuneService;
private String emailAddress;
private String team;

public CricketCoach() {
System.out.println("CricketCoach: inside no-arg constructor");
}

public void setFortuneService(FortuneService fortuneService) {
System.out.println("CricketCoach: inside our setter method - setFortuneService");
this.fortuneService = fortuneService;
}

@Override
public String getDailyWorkout() {
return "Practice fast bowling for 15minutes";
}

@Override
public String getDailyFortune() {
return fortuneService.getFortune();
}

}
24 changes: 24 additions & 0 deletions spring-demo-1/src/com/luv2code/springdemo/SetterDemoApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.luv2code.springdemo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SetterDemoApp {

public static void main(String[] args) {

// load the spring configuration file
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

// retrieve bean from Spring Container
CricketCoach theCoach = ctx.getBean("myCricketCoach", CricketCoach.class);

// call methods on bean object
System.out.println(theCoach.getDailyWorkout());
System.out.println(theCoach.getDailyFortune());

// close the context nicely
ctx.close();

}

}

0 comments on commit 548824f

Please sign in to comment.