Skip to content

Commit

Permalink
evolve(demo1):dependency injection - constructor injection
Browse files Browse the repository at this point in the history
  • Loading branch information
chengcyber committed Dec 25, 2017
1 parent fc94a15 commit 27b8153
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 0 deletions.
8 changes: 8 additions & 0 deletions spring-demo-1/src/applicationContext.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,16 @@
http://www.springframework.org/schema/context/spring-context.xsd">

<!-- Define your beans here -->

<bean id="myFortuneService"
class="com.luv2code.springdemo.HappyFortuneService"></bean>

<bean id="myCoach"
class="com.luv2code.springdemo.TrackCoach">

<!-- set up constrcutor injection -->
<constructor-arg ref="myFortuneService"></constructor-arg>

</bean>

</beans>
Expand Down
13 changes: 13 additions & 0 deletions spring-demo-1/src/com/luv2code/springdemo/BaseballCoach.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,22 @@

public class BaseballCoach implements Coach {

// define a private field for dependency
private FortuneService fortuneService;

// define a constructor for dependency injection
public BaseballCoach(FortuneService theFortuneService) {
fortuneService = theFortuneService;
}

@Override
public String getDailyWorkout() {
return "Spend 30 minutes on batting practice";
}

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

}
1 change: 1 addition & 0 deletions spring-demo-1/src/com/luv2code/springdemo/Coach.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ public interface Coach {

public String getDailyWorkout();

public String getDailyFortune();
}
6 changes: 6 additions & 0 deletions spring-demo-1/src/com/luv2code/springdemo/FortuneService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.luv2code.springdemo;

public interface FortuneService {

public String getFortune();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.luv2code.springdemo;

public class HappyFortuneService implements FortuneService {

@Override
public String getFortune() {
return "This a your lucky day";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public static void main(String[] args) {

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

// close the context
context.close();
Expand Down
16 changes: 16 additions & 0 deletions spring-demo-1/src/com/luv2code/springdemo/TrackCoach.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,25 @@

public class TrackCoach implements Coach {

private FortuneService fortuneService;

public TrackCoach() {

}

public TrackCoach(FortuneService fortuneService) {
super();
this.fortuneService = fortuneService;
}

@Override
public String getDailyWorkout() {
return "Run a hard 5K";
}

@Override
public String getDailyFortune() {
return "Just do it: " + fortuneService.getFortune();
}

}

0 comments on commit 27b8153

Please sign in to comment.