Browse Source

spring

master
Winnie 3 years ago
parent
commit
7c595d76f3

+ 6
- 0
pom.xml View File

@@ -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>

+ 13
- 7
src/main/java/be/syntra/App.java View File

@@ -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();
}

}
}

+ 1
- 1
src/main/java/be/syntra/Coach.java View File

@@ -4,4 +4,4 @@ public interface Coach {
String getDailyWorkout();

String getDailyMeal();
}
}

+ 2
- 2
src/main/java/be/syntra/DietService.java View File

@@ -1,5 +1,5 @@
package be.syntra;

interface DietService {
public interface DietService {
String getRecommendation();
}
}

+ 2
- 2
src/main/java/be/syntra/PaleoService.java View File

@@ -3,6 +3,6 @@ package be.syntra;
public class PaleoService implements DietService{
@Override
public String getRecommendation() {
return "Eat Meat";
return "Eat Meat";
}
}
}

+ 2
- 2
src/main/java/be/syntra/SwimCoach.java View File

@@ -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 {
}


}
}

+ 1
- 1
src/main/java/be/syntra/TennisCoach.java View File

@@ -5,7 +5,7 @@ public class TennisCoach implements Coach {

private DietService dietService;

public TennisCoach() {
public TennisCoach(DietService dietService) {
this.dietService = new VeganService();
}


+ 1
- 1
src/main/java/be/syntra/VeggieService.java View File

@@ -6,4 +6,4 @@ public class VeggieService implements DietService {
public String getRecommendation() {
return "Salad and veggie wraps";
}
}
}

+ 23
- 0
src/main/java/be/syntra/common/AppConfig.java View File

@@ -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());
}


}

Loading…
Cancel
Save