Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correcting instances of greeting services in test, according to the DIs #121

Open
wants to merge 2 commits into
base: primary-bean
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package guru.springframework.sfgdi;

import guru.springframework.sfgdi.controllers.ConstructorInjectedController;
import guru.springframework.sfgdi.controllers.MyController;
import guru.springframework.sfgdi.controllers.PropertyInjectedController;
import guru.springframework.sfgdi.controllers.SetterInjectedController;
import guru.springframework.sfgdi.controllers.*;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
Expand All @@ -14,6 +11,9 @@ public class SfgDiApplication {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(SfgDiApplication.class, args);

I18nController i18nController = (I18nController) ctx.getBean("i18nController");
System.out.println(i18nController.sayHello());

MyController myController = (MyController) ctx.getBean("myController");

System.out.println("------- Primary Bean");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public class ConstructorInjectedController {
private final GreetingService greetingService;

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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 I18nController {
private final GreetingService greetingService;

public I18nController(@Qualifier("i18nService") GreetingService greetingService) {
// there are two services named "i18nService"
// we added the @Profile annotaion to make the output dependent of the users active profile
// without the profile, Spring wouldn't know which one to get --> throws error
this.greetingService = greetingService;
}

public String sayHello(){
return greetingService.sayGreeting();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Created by jt on 12/26/19.
*/
@Service
public class ConstructorGreetingService implements GreetingService {
public class ConstructorInjectedGreetingService implements GreetingService {
@Override
public String sayGreeting() {
return "Hello World - Constructor";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package guru.springframework.sfgdi.services;

import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;

@Profile("EN")
// there are two services named "i18nService"
// we added the @Profile annotaion to make the output dependent of the users active profile
// without the profile, Spring wouldn't know which one to get --> throws error
@Service("i18nService")
public class I18nEnglishGreetingService implements GreetingService{
@Override
public String sayGreeting(){
return "Hello World – EN";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package guru.springframework.sfgdi.services;

import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;

@Profile("ES")
// there are two services named "i18nService"
// we added the @Profile annotaion to make the output dependent of the users active profile
// without the profile, Spring wouldn't know which one to get --> throws error
@Service("i18nService")
public class I18nSpanishGreetingService implements GreetingService {
@Override
public String sayGreeting(){
return "Hola Mundo – ES";
}
}
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@

spring.profiles.active=ES
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package guru.springframework.sfgdi.controllers;

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

Expand All @@ -10,7 +10,7 @@ class ConstructorInjectedControllerTest {

@BeforeEach
void setUp() {
controller = new ConstructorInjectedController(new ConstructorGreetingService());
controller = new ConstructorInjectedController(new ConstructorInjectedGreetingService());
}

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

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

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

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

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

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

Expand All @@ -11,7 +11,7 @@ class SetterInjectedControllerTest {
@BeforeEach
void setUp() {
controller = new SetterInjectedController();
controller.setGreetingService(new ConstructorGreetingService());
controller.setGreetingService(new SetterInjectedGreetingService());
}

@Test
Expand Down