Skip to content

Commit

Permalink
Agrega ejemplos de DI con Qualifier
Browse files Browse the repository at this point in the history
  • Loading branch information
mdmg92 committed Mar 30, 2020
1 parent 2082c6e commit 769676d
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package guru.springframework.sfgdi.controllers;

import guru.springframework.sfgdi.services.GreetingService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;

@Controller
public class ConstructorInjectedController {

private final GreetingService greetingService;

public ConstructorInjectedController(GreetingService greetingService) {
public ConstructorInjectedController(@Qualifier("constructorGreetingService") GreetingService greetingService) {
this.greetingService = greetingService;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import guru.springframework.sfgdi.services.GreetingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;

@Controller
public class PropertyInjectedController {

@Autowired
@Qualifier("propertyInjectedGreetingService")
public GreetingService greetingService;

public String getGretting() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import guru.springframework.sfgdi.services.GreetingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;

@Controller
Expand All @@ -11,6 +12,7 @@ public class SetterInjectedController {
private GreetingService greetingService;

@Autowired
@Qualifier("setterInjectedGreetingService")
public void setGreetingService(GreetingService greetingService) {
this.greetingService = greetingService;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package guru.springframework.sfgdi.services;

import org.springframework.stereotype.Service;

@Service
public class ConstructorGreetingService implements GreetingService {
@Override
public String sayGreeting() {
return "Hello World! - Constructor";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package guru.springframework.sfgdi.services;

import org.springframework.stereotype.Service;

@Service
public class PropertyInjectedGreetingService implements GreetingService {
@Override
public String sayGreeting() {
return "Hello World! - Property";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import org.springframework.stereotype.Service;

@Service
public class GreetingServiceImpl implements GreetingService {
public class SetterInjectedGreetingService implements GreetingService {
@Override
public String sayGreeting() {
return "Hello World!";
return "Hello World! - Setter";
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package guru.springframework.sfgdi.controllers;

import guru.springframework.sfgdi.services.GreetingServiceImpl;
import guru.springframework.sfgdi.services.ConstructorGreetingService;
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());
controller = new ConstructorInjectedController(new ConstructorGreetingService());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package guru.springframework.sfgdi.controllers;

import guru.springframework.sfgdi.services.GreetingServiceImpl;
import guru.springframework.sfgdi.services.ConstructorGreetingService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class PropertyInjectedControllerTest {

PropertyInjectedController controller;
Expand All @@ -14,7 +12,7 @@ class PropertyInjectedControllerTest {
void setUp() {
controller = new PropertyInjectedController();

controller.greetingService = new GreetingServiceImpl();
controller.greetingService = new ConstructorGreetingService();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package guru.springframework.sfgdi.controllers;

import guru.springframework.sfgdi.services.GreetingServiceImpl;
import guru.springframework.sfgdi.services.ConstructorGreetingService;
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());
controller.setGreetingService(new ConstructorGreetingService());
}

@Test
Expand Down

0 comments on commit 769676d

Please sign in to comment.