Skip to content
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
15 changes: 14 additions & 1 deletion src/main/java/guru/springframework/DiDemoApplication.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
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");

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;
}
}
16 changes: 16 additions & 0 deletions src/main/java/guru/springframework/controllers/MyController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package guru.springframework.controllers;

import org.springframework.stereotype.Controller;

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

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

return "foo";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package guru.springframework.controllers;

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


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

@Autowired
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,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!!!!";

@Override
public String sayGreeting() {
return HELLO_GURUS;
}
}
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());
}
}