Spring Data Injection Demo
Spring Data Injection Demo using annotations
This commit is contained in:
+12
@@ -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);
|
||||
}
|
||||
}
|
||||
+45
@@ -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<Order> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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..
|
||||
|
||||
}
|
||||
+25
@@ -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<Order> getCustomerOrders(Long customerId) {
|
||||
return orderService.getOrdersForCustomer(customerId);
|
||||
}
|
||||
|
||||
}
|
||||
+20
@@ -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<Order> getCustomerOrders(Long customerId) {
|
||||
return orderService.getOrdersForCustomer(customerId);
|
||||
}
|
||||
|
||||
}
|
||||
+19
@@ -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<Order> getCustomerOrders(Long customerId) {
|
||||
return orderService.getOrdersForCustomer(customerId);
|
||||
}
|
||||
}
|
||||
+24
@@ -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<Order> getCustomerOrders(Long customerId) {
|
||||
return orderService.getOrdersForCustomer(customerId);
|
||||
}
|
||||
|
||||
@Autowired
|
||||
public void setOrderService(OrderService orderService) {
|
||||
this.orderService = orderService;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<Order> getOrdersForCustomer(Long id) {
|
||||
List<Order> orders = new ArrayList<Order>();
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user