Skip to content

Commit

Permalink
adding manual DI example
Browse files Browse the repository at this point in the history
  • Loading branch information
springframeworkguru committed Dec 26, 2019
1 parent 5aa8911 commit c21a92b
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 0 deletions.
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();
}
}
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();
}

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

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

}
}

1 comment on commit c21a92b

@Quazi-Mohammed-Farhan
Copy link

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.

Please sign in to comment.