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

Di assignment #3

Open
wants to merge 15 commits into
base: master
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.M1</version>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

Expand Down
14 changes: 13 additions & 1 deletion src/main/java/guru/springframework/DiDemoApplication.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
package guru.springframework;

import guru.springframework.controllers.ConstructorInjectedController;
import guru.springframework.controllers.GetterInjectedController;
import guru.springframework.controllers.MyController;
import guru.springframework.controllers.PropertyInjectedController;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class DiDemoApplication {

public static void main(String[] args) {
SpringApplication.run(DiDemoApplication.class, args);
ApplicationContext ctx = SpringApplication.run(DiDemoApplication.class, args);

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

System.out.println(controller.hello());
System.out.println(ctx.getBean(PropertyInjectedController.class).sayHello());
System.out.println(ctx.getBean(GetterInjectedController.class).sayHello());
System.out.println(ctx.getBean(ConstructorInjectedController.class).sayHello());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package guru.springframework.controllers;

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

/**
* Created by jt on 5/24/17.
*/
@Controller
public class ConstructorInjectedController {

private GreetingService greetingService;

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

public String sayHello(){
return greetingService.sayGreeting();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package guru.springframework.controllers;

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

/**
* Created by jt on 5/24/17.
*/
@Controller
public class GetterInjectedController {
private GreetingService greetingService;

public String sayHello(){
return greetingService.sayGreeting();
}

@Autowired
public void setGreetingService(@Qualifier("getterGreetingService") GreetingService greetingService) {
this.greetingService = greetingService;
}
}
23 changes: 23 additions & 0 deletions src/main/java/guru/springframework/controllers/MyController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package guru.springframework.controllers;

import guru.springframework.services.GreetingService;
import org.springframework.stereotype.Controller;

/**
* Created by jt on 5/23/17.
*/
@Controller
public class MyController {

private GreetingService greetingService;

public MyController(GreetingService greetingService) {
this.greetingService = greetingService;
}

public String hello(){
System.out.println("Hello!!! ");

return greetingService.sayGreeting();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package guru.springframework.controllers;

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


/**
* Created by jt on 5/24/17.
*/
@Controller
public class PropertyInjectedController {

@Autowired
@Qualifier("greetingServiceImpl")
public GreetingService greetingServiceImpl;

public String sayHello(){
return greetingServiceImpl.sayGreeting();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package guru.springframework.services;

import org.springframework.stereotype.Service;

/**
* Created by jt on 5/24/17.
*/
@Service
public class ConstructorGreetingService implements GreetingService {
@Override
public String sayGreeting() {
return "Hello - I was injected via the constructor!!!";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package guru.springframework.services;

import org.springframework.stereotype.Service;

/**
* Created by jt on 5/24/17.
*/
@Service
public class GetterGreetingService implements GreetingService {

@Override
public String sayGreeting() {
return "Hello - I was injected by the getter";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package guru.springframework.services;

/**
* Created by jt on 5/24/17.
*/
public interface GreetingRepository {

String getEnglishGreeting();

String getSpanishGreeting();

String getGermanGreeting();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package guru.springframework.services;

/**
* Created by jt on 5/24/17.
*/
public class GreetingRepositoryImpl implements GreetingRepository {

@Override
public String getEnglishGreeting() {
return "Hello - Primary Greeting service";
}

@Override
public String getSpanishGreeting() {
return "Servicio de Saludo Primario";
}

@Override
public String getGermanGreeting() {

return "Primärer Grußdienst";

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package guru.springframework.services;

/**
* Created by jt on 5/24/17.
*/
public interface GreetingService {

String sayGreeting();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package guru.springframework.services;

import org.springframework.stereotype.Service;

/**
* Created by jt on 5/24/17.
*/
@Service
public class GreetingServiceImpl implements GreetingService {

public static final String HELLO_GURUS = "Hello Gurus!!!! - Original";

@Override
public String sayGreeting() {
return HELLO_GURUS;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package guru.springframework.services;

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

/**
* Created by jt on 5/24/17.
*/
@Service
@Primary
@Profile("de")
public class PrimaryGermanGreetingService implements GreetingService {

private GreetingRepository greetingRepository;

@Override
public String sayGreeting() {
return greetingRepository.getGermanGreeting();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package guru.springframework.services;

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

/**
* Created by jt on 5/24/17.
*/
@Service
@Primary
@Profile({"en", "default"})
public class PrimaryGreetingService implements GreetingService {

private GreetingRepository greetingRepository;

@Override
public String sayGreeting() {
return greetingRepository.getEnglishGreeting();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package guru.springframework.services;

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

/**
* Created by jt on 5/24/17.
*/
@Service
@Profile("es")
@Primary
public class PrimarySpanishGreetingService implements GreetingService {

private GreetingRepository greetingRepository;

@Override
public String sayGreeting() {
return greetingRepository.getSpanishGreeting();
}
}
1 change: 1 addition & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spring.profiles.active=de
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package guru.springframework.controllers;

import guru.springframework.services.GreetingServiceImpl;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
* Created by jt on 5/24/17.
*/
public class ConstructorInjectedControllerTest {
private ConstructorInjectedController constructorInjectedController;

@Before
public void setUp() throws Exception {
this.constructorInjectedController = new ConstructorInjectedController(new GreetingServiceImpl());
}

@Test
public void testGreeting() throws Exception {
assertEquals(GreetingServiceImpl.HELLO_GURUS, constructorInjectedController.sayHello());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package guru.springframework.controllers;

import guru.springframework.services.GreetingServiceImpl;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
* Created by jt on 5/24/17.
*/
public class GetterInjectedControllerTest {

private GetterInjectedController getterInjectedController;

@Before
public void setUp() throws Exception {
this.getterInjectedController = new GetterInjectedController();
this.getterInjectedController.setGreetingService(new GreetingServiceImpl());
}

@Test
public void testGreeting() throws Exception {
assertEquals(GreetingServiceImpl.HELLO_GURUS, getterInjectedController.sayHello());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package guru.springframework.controllers;

import guru.springframework.services.GreetingServiceImpl;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
* Created by jt on 5/24/17.
*/
public class PropertyInjectedControllerTest {

private PropertyInjectedController propertyInjectedController;

@Before
public void setUp() throws Exception {
this.propertyInjectedController = new PropertyInjectedController();
this.propertyInjectedController.greetingServiceImpl = new GreetingServiceImpl();
}

@Test
public void testGreeting() throws Exception {
assertEquals(GreetingServiceImpl.HELLO_GURUS, propertyInjectedController.sayHello());
}
}