-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5aa8911
commit c21a92b
Showing
8 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
src/main/java/guru/springframework/sfgdi/controllers/ConstructorInjectedController.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,18 @@ | ||
package guru.springframework.sfgdi.controllers; | ||
|
||
import guru.springframework.sfgdi.services.GreetingService; | ||
|
||
/** | ||
* Created by jt on 12/26/19. | ||
*/ | ||
public class ConstructorInjectedController { | ||
private final GreetingService greetingService; | ||
|
||
public ConstructorInjectedController(GreetingService greetingService) { | ||
this.greetingService = greetingService; | ||
} | ||
|
||
public String getGreeting(){ | ||
return greetingService.sayGreeting(); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/guru/springframework/sfgdi/controllers/PropertyInjectedController.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,16 @@ | ||
package guru.springframework.sfgdi.controllers; | ||
|
||
import guru.springframework.sfgdi.services.GreetingService; | ||
|
||
/** | ||
* Created by jt on 12/26/19. | ||
*/ | ||
public class PropertyInjectedController { | ||
|
||
public GreetingService greetingService; | ||
|
||
public String getGreeting(){ | ||
return greetingService.sayGreeting(); | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/guru/springframework/sfgdi/controllers/SetterInjectedController.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,19 @@ | ||
package guru.springframework.sfgdi.controllers; | ||
|
||
import guru.springframework.sfgdi.services.GreetingService; | ||
|
||
/** | ||
* Created by jt on 12/26/19. | ||
*/ | ||
public class SetterInjectedController { | ||
|
||
private GreetingService greetingService; | ||
|
||
public void setGreetingService(GreetingService greetingService) { | ||
this.greetingService = greetingService; | ||
} | ||
|
||
public String getGreeting(){ | ||
return greetingService.sayGreeting(); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/guru/springframework/sfgdi/services/GreetingService.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,9 @@ | ||
package guru.springframework.sfgdi.services; | ||
|
||
/** | ||
* Created by jt on 12/26/19. | ||
*/ | ||
public interface GreetingService { | ||
|
||
String sayGreeting(); | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/guru/springframework/sfgdi/services/GreetingServiceImpl.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,11 @@ | ||
package guru.springframework.sfgdi.services; | ||
|
||
/** | ||
* Created by jt on 12/26/19. | ||
*/ | ||
public class GreetingServiceImpl implements GreetingService { | ||
@Override | ||
public String sayGreeting() { | ||
return "Hello World"; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/test/java/guru/springframework/sfgdi/controllers/ConstructorInjectedControllerTest.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 guru.springframework.sfgdi.controllers; | ||
|
||
import guru.springframework.sfgdi.services.GreetingServiceImpl; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class ConstructorInjectedControllerTest { | ||
|
||
ConstructorInjectedController controller; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
controller = new ConstructorInjectedController(new GreetingServiceImpl()); | ||
} | ||
|
||
@Test | ||
void getGreeting() { | ||
|
||
System.out.println(controller.getGreeting()); | ||
|
||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/test/java/guru/springframework/sfgdi/controllers/PropertyInjectedControllerTest.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,25 @@ | ||
package guru.springframework.sfgdi.controllers; | ||
|
||
import guru.springframework.sfgdi.services.GreetingServiceImpl; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class PropertyInjectedControllerTest { | ||
|
||
PropertyInjectedController controller; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
controller = new PropertyInjectedController(); | ||
|
||
controller.greetingService = new GreetingServiceImpl(); | ||
} | ||
|
||
@Test | ||
void getGreeting() { | ||
|
||
System.out.println(controller.getGreeting()); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/test/java/guru/springframework/sfgdi/controllers/SetterInjectedControllerTest.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 guru.springframework.sfgdi.controllers; | ||
|
||
import guru.springframework.sfgdi.services.GreetingServiceImpl; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class SetterInjectedControllerTest { | ||
|
||
SetterInjectedController controller; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
controller = new SetterInjectedController(); | ||
controller.setGreetingService(new GreetingServiceImpl()); | ||
} | ||
|
||
@Test | ||
void getGreeting() { | ||
System.out.println(controller.getGreeting()); | ||
|
||
} | ||
} |
c21a92b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the ConstructorInjectedController use-case, the GreetingService declaration should be final but it is not. I think in the lecture it was said that the property should be declared final for constructor injection.