@@ -15,6 +15,12 @@ | |||
</properties> | |||
<dependencies> | |||
<!-- https://mvnrepository.com/artifact/org.springframework/spring --> | |||
<dependency> | |||
<groupId>org.springframework</groupId> | |||
<artifactId>spring-context</artifactId> | |||
<version>5.2.13.RELEASE</version> | |||
</dependency> | |||
<dependency> | |||
<groupId>org.junit.jupiter</groupId> | |||
<artifactId>junit-jupiter</artifactId> |
@@ -1,17 +1,23 @@ | |||
package be.syntra; | |||
import be.syntra.common.AppConfig; | |||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | |||
public class App { | |||
public static void main(String[] args) { | |||
public static void main(String[] args) { | |||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); | |||
Coach coach = context.getBean("coach",Coach.class); | |||
Coach coach = new SwimCoach(); | |||
//call method from bean | |||
System.out.println(coach.getDailyWorkout()); | |||
//call method from bean | |||
System.out.println(coach.getDailyWorkout()); | |||
//get diet recommendation | |||
System.out.println(coach.getDailyMeal()); | |||
//get diet recommendation | |||
System.out.println(coach.getDailyMeal()); | |||
context.close(); | |||
} | |||
} | |||
} |
@@ -4,4 +4,4 @@ public interface Coach { | |||
String getDailyWorkout(); | |||
String getDailyMeal(); | |||
} | |||
} |
@@ -1,5 +1,5 @@ | |||
package be.syntra; | |||
interface DietService { | |||
public interface DietService { | |||
String getRecommendation(); | |||
} | |||
} |
@@ -3,6 +3,6 @@ package be.syntra; | |||
public class PaleoService implements DietService{ | |||
@Override | |||
public String getRecommendation() { | |||
return "Eat Meat"; | |||
return "Eat Meat"; | |||
} | |||
} | |||
} |
@@ -4,7 +4,7 @@ public class SwimCoach implements Coach { | |||
private DietService dietService; | |||
public SwimCoach() { | |||
public SwimCoach(DietService dietService) { | |||
this.dietService = new VeggieService(); | |||
} | |||
@@ -19,4 +19,4 @@ public class SwimCoach implements Coach { | |||
} | |||
} | |||
} |
@@ -5,7 +5,7 @@ public class TennisCoach implements Coach { | |||
private DietService dietService; | |||
public TennisCoach() { | |||
public TennisCoach(DietService dietService) { | |||
this.dietService = new VeganService(); | |||
} | |||
@@ -6,4 +6,4 @@ public class VeggieService implements DietService { | |||
public String getRecommendation() { | |||
return "Salad and veggie wraps"; | |||
} | |||
} | |||
} |
@@ -0,0 +1,23 @@ | |||
package be.syntra.common; | |||
import be.syntra.*; | |||
import org.springframework.context.annotation.Bean; | |||
import org.springframework.context.annotation.Configuration; | |||
@Configuration | |||
public class AppConfig { | |||
@Bean | |||
public DietService dietService() { //the method name is the "Bean id" | |||
return new VeggieService(); | |||
} | |||
//Define Bean for SwimCoach and Dependency Injection | |||
@Bean | |||
public Coach coach() { //-> the method name is the "Bean id" | |||
return new SwimCoach(dietService()); | |||
} | |||
} |