-
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(annotation): java code config - inner defined bean
- Loading branch information
1 parent
f7f3ceb
commit 667c8bb
Showing
4 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
spring-demo-annotations/src/com/luv2code/springdemo/SadFortuneService.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,10 @@ | ||
package com.luv2code.springdemo; | ||
|
||
public class SadFortuneService implements FortuneService { | ||
|
||
@Override | ||
public String getFortune() { | ||
return "Today is totaly a sad day ..."; | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
spring-demo-annotations/src/com/luv2code/springdemo/SportConfig.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 |
---|---|---|
@@ -1,11 +1,24 @@ | ||
package com.luv2code.springdemo; | ||
|
||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.ComponentScan; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
@ComponentScan("com.luv2code.springdemo") | ||
public class SportConfig { | ||
|
||
// define bean for our sad fortune service | ||
@Bean | ||
public FortuneService sadFortuneService() { | ||
return new SadFortuneService(); | ||
} | ||
|
||
// define bean for an swim coach AND inject dependency | ||
@Bean | ||
public Coach swimCoach() { | ||
return new SwimCoach(sadFortuneService()); | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
spring-demo-annotations/src/com/luv2code/springdemo/SwimCoach.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,21 @@ | ||
package com.luv2code.springdemo; | ||
|
||
public class SwimCoach implements Coach { | ||
|
||
private FortuneService fortuneService; | ||
|
||
public SwimCoach(FortuneService theFortuneService) { | ||
fortuneService = theFortuneService; | ||
} | ||
|
||
@Override | ||
public String getDailyWorkout() { | ||
return "Swim 1000 meters as a warm up."; | ||
} | ||
|
||
@Override | ||
public String getDailyFortune() { | ||
return fortuneService.getFortune(); | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
spring-demo-annotations/src/com/luv2code/springdemo/SwimJavaConfigDemoApp.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,27 @@ | ||
package com.luv2code.springdemo; | ||
|
||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | ||
|
||
public class SwimJavaConfigDemoApp { | ||
|
||
public static void main(String[] args) { | ||
|
||
// read spring config java class | ||
AnnotationConfigApplicationContext ctx = | ||
new AnnotationConfigApplicationContext(SportConfig.class); | ||
|
||
// get the bean from spring container | ||
Coach theCoach = ctx.getBean("swimCoach", Coach.class); | ||
|
||
// call a method on the bean | ||
System.out.println(theCoach.getDailyWorkout()); | ||
|
||
// call a method with dependency injection | ||
System.out.println(theCoach.getDailyFortune()); | ||
|
||
// close the context | ||
ctx.close(); | ||
|
||
} | ||
|
||
} |