diff --git a/SpringDataInjectionDemo/pom.xml b/SpringDataInjectionDemo/pom.xml new file mode 100644 index 0000000000..5b70549079 --- /dev/null +++ b/SpringDataInjectionDemo/pom.xml @@ -0,0 +1,44 @@ + + + 4.0.0 + + baeldung + springdatainjectiondemo + 0.0.1-SNAPSHOT + jar + + SpringDataInjectionDemo + Spring Data Injection Demp + + + org.springframework.boot + spring-boot-starter-parent + 1.5.3.RELEASE + + + + + UTF-8 + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + diff --git a/SpringDataInjectionDemo/src/main/java/com/baeldung/SpringDataInjectionDemoApplication.java b/SpringDataInjectionDemo/src/main/java/com/baeldung/SpringDataInjectionDemoApplication.java new file mode 100644 index 0000000000..5a166bf9f9 --- /dev/null +++ b/SpringDataInjectionDemo/src/main/java/com/baeldung/SpringDataInjectionDemoApplication.java @@ -0,0 +1,12 @@ +package com.baeldung; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringDataInjectionDemoApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringDataInjectionDemoApplication.class, args); + } +} diff --git a/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/controller/OrderController.java b/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/controller/OrderController.java new file mode 100644 index 0000000000..f66c1e0928 --- /dev/null +++ b/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/controller/OrderController.java @@ -0,0 +1,45 @@ +package com.baeldung.didemo.controller; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.didemo.model.Order; +import com.baeldung.didemo.service.CustomerServiceConstructorDI; +import com.baeldung.didemo.service.CustomerServiceConstructorWithoutSpringDI; +import com.baeldung.didemo.service.CustomerServiceFieldDI; +import com.baeldung.didemo.service.CustomerServiceSetterDI; +import com.baeldung.didemo.service.OrderService; + +@RestController +@RequestMapping(value = "/orders") +public class OrderController { + + @Autowired + CustomerServiceConstructorDI constructorDI; + + @Autowired + CustomerServiceFieldDI fieldDI; + + @Autowired + CustomerServiceSetterDI setterDI; + + @RequestMapping(method = RequestMethod.GET) + public List getOrdersFieldDI(@RequestParam(required = false) String dIMethod) { + if ("setter".equals(dIMethod)) { + return setterDI.getCustomerOrders(1l); + } else if ("constructor".equals(dIMethod)) { + return constructorDI.getCustomerOrders(1l); + } else if ("field".equals(dIMethod)) { + return fieldDI.getCustomerOrders(1l); + } else { + OrderService orderSvc = new OrderService(); + CustomerServiceConstructorWithoutSpringDI customerSvc = new CustomerServiceConstructorWithoutSpringDI(orderSvc); + return customerSvc.getCustomerOrders(1l); + } + } +} diff --git a/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/model/Order.java b/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/model/Order.java new file mode 100644 index 0000000000..9fd3710879 --- /dev/null +++ b/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/model/Order.java @@ -0,0 +1,26 @@ +package com.baeldung.didemo.model; + +public class Order { + + private Integer id; + private String item; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getItem() { + return item; + } + + public void setItem(String item) { + this.item = item; + } + + // other order properties.. + +} diff --git a/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/service/CustomerServiceConstructorDI.java b/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/service/CustomerServiceConstructorDI.java new file mode 100644 index 0000000000..daf551b299 --- /dev/null +++ b/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/service/CustomerServiceConstructorDI.java @@ -0,0 +1,25 @@ +package com.baeldung.didemo.service; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.baeldung.didemo.model.Order; + +@Service +public class CustomerServiceConstructorDI { + + OrderService orderService; + + @Autowired + public CustomerServiceConstructorDI(OrderService orderService) { + super(); + this.orderService = orderService; + } + + public List getCustomerOrders(Long customerId) { + return orderService.getOrdersForCustomer(customerId); + } + +} diff --git a/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/service/CustomerServiceConstructorWithoutSpringDI.java b/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/service/CustomerServiceConstructorWithoutSpringDI.java new file mode 100644 index 0000000000..d61b9f86cc --- /dev/null +++ b/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/service/CustomerServiceConstructorWithoutSpringDI.java @@ -0,0 +1,20 @@ +package com.baeldung.didemo.service; + +import java.util.List; + +import com.baeldung.didemo.model.Order; + +public class CustomerServiceConstructorWithoutSpringDI { + + OrderService orderService; + + public CustomerServiceConstructorWithoutSpringDI(OrderService orderService) { + super(); + this.orderService = orderService; + } + + public List getCustomerOrders(Long customerId) { + return orderService.getOrdersForCustomer(customerId); + } + +} diff --git a/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/service/CustomerServiceFieldDI.java b/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/service/CustomerServiceFieldDI.java new file mode 100644 index 0000000000..acb22ad529 --- /dev/null +++ b/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/service/CustomerServiceFieldDI.java @@ -0,0 +1,19 @@ +package com.baeldung.didemo.service; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.baeldung.didemo.model.Order; + +@Service +public class CustomerServiceFieldDI { + + @Autowired + OrderService orderService; + + public List getCustomerOrders(Long customerId) { + return orderService.getOrdersForCustomer(customerId); + } +} diff --git a/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/service/CustomerServiceSetterDI.java b/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/service/CustomerServiceSetterDI.java new file mode 100644 index 0000000000..736a43aba4 --- /dev/null +++ b/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/service/CustomerServiceSetterDI.java @@ -0,0 +1,24 @@ +package com.baeldung.didemo.service; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.baeldung.didemo.model.Order; + +@Service +public class CustomerServiceSetterDI { + + OrderService orderService; + + public List getCustomerOrders(Long customerId) { + return orderService.getOrdersForCustomer(customerId); + } + + @Autowired + public void setOrderService(OrderService orderService) { + this.orderService = orderService; + } + +} diff --git a/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/service/OrderService.java b/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/service/OrderService.java new file mode 100644 index 0000000000..4dc8fd6f5d --- /dev/null +++ b/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/service/OrderService.java @@ -0,0 +1,30 @@ +package com.baeldung.didemo.service; + +import java.util.ArrayList; +import java.util.List; + +import org.springframework.stereotype.Service; + +import com.baeldung.didemo.model.Order; + +@Service +public class OrderService { + + public List getOrdersForCustomer(Long id) { + List orders = new ArrayList(); + + Order order1 = new Order(); + order1.setId(1); + order1.setItem("Pizza"); + + Order order2 = new Order(); + order2.setId(1); + order2.setItem("Garlic Bread"); + + orders.add(order1); + orders.add(order2); + + return orders; + } + +} diff --git a/SpringDataInjectionDemo/src/main/resources/application.properties b/SpringDataInjectionDemo/src/main/resources/application.properties new file mode 100644 index 0000000000..e69de29bb2