-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
evolve(demo1):dependency injection - setter injection
- Loading branch information
1 parent
a770408
commit 548824f
Showing
3 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
spring-demo-1/src/com/luv2code/springdemo/CricketCoach.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
24
spring-demo-1/src/com/luv2code/springdemo/SetterDemoApp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} | ||
|
||
} |