Spring Data Injection Demo
Spring Data Injection Demo using annotations
This commit is contained in:
parent
faa15d50ce
commit
140de5b0b2
SpringDataInjectionDemo
pom.xml
src/main
44
SpringDataInjectionDemo/pom.xml
Normal file
44
SpringDataInjectionDemo/pom.xml
Normal file
@ -0,0 +1,44 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>baeldung</groupId>
|
||||
<artifactId>springdatainjectiondemo</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>SpringDataInjectionDemo</name>
|
||||
<description>Spring Data Injection Demp</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.5.3.RELEASE</version>
|
||||
<relativePath /> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
12
SpringDataInjectionDemo/src/main/java/com/baeldung/SpringDataInjectionDemoApplication.java
Normal file
12
SpringDataInjectionDemo/src/main/java/com/baeldung/SpringDataInjectionDemoApplication.java
Normal file
@ -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
SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/controller/OrderController.java
Normal file
45
SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/controller/OrderController.java
Normal file
@ -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
SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/service/CustomerServiceConstructorDI.java
Normal file
25
SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/service/CustomerServiceConstructorDI.java
Normal file
@ -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
SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/service/CustomerServiceConstructorWithoutSpringDI.java
Normal file
20
SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/service/CustomerServiceConstructorWithoutSpringDI.java
Normal file
@ -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
SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/service/CustomerServiceFieldDI.java
Normal file
19
SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/service/CustomerServiceFieldDI.java
Normal file
@ -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
SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/service/CustomerServiceSetterDI.java
Normal file
24
SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/service/CustomerServiceSetterDI.java
Normal file
@ -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;
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user