From 0dab47c76d3a5c8e6a8239929301e86b7a993545 Mon Sep 17 00:00:00 2001 From: Senthil Kumar Subramanian Date: Sun, 25 Mar 2018 21:06:38 +0530 Subject: [PATCH] Spring-dependency injection --- Spring-DependencyInjection/pom.xml | 30 +++++++++ .../src/main/java/beans.xml | 31 ++++++++++ .../com/baeldung/spring/core/Address.java | 53 ++++++++++++++++ .../java/com/baeldung/spring/core/Car.java | 30 +++++++++ .../com/baeldung/spring/core/Customer.java | 61 +++++++++++++++++++ .../com/baeldung/spring/core/Department.java | 31 ++++++++++ .../com/baeldung/spring/core/Employee.java | 23 +++++++ .../com/baeldung/spring/core/Product.java | 48 +++++++++++++++ .../com/baeldung/spring/core/Student.java | 40 ++++++++++++ .../com/baeldung/spring/core/Subject.java | 30 +++++++++ .../spring/core/config/AppConfig.java | 30 +++++++++ .../AnnotationApplicationContextTest.java | 17 ++++++ .../JavaConfigApplicationContextTest.java | 23 +++++++ ...MLConfigurationApplicationContextTest.java | 28 +++++++++ .../Spring_DependencyInjection/AppTest.java | 38 ++++++++++++ 15 files changed, 513 insertions(+) create mode 100644 Spring-DependencyInjection/pom.xml create mode 100644 Spring-DependencyInjection/src/main/java/beans.xml create mode 100644 Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Address.java create mode 100644 Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Car.java create mode 100644 Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Customer.java create mode 100644 Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Department.java create mode 100644 Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Employee.java create mode 100644 Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Product.java create mode 100644 Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Student.java create mode 100644 Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Subject.java create mode 100644 Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/config/AppConfig.java create mode 100644 Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/test/AnnotationApplicationContextTest.java create mode 100644 Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/test/JavaConfigApplicationContextTest.java create mode 100644 Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/test/XMLConfigurationApplicationContextTest.java create mode 100644 Spring-DependencyInjection/src/test/java/com/spring/core/Spring_DependencyInjection/AppTest.java diff --git a/Spring-DependencyInjection/pom.xml b/Spring-DependencyInjection/pom.xml new file mode 100644 index 0000000000..cc818f948b --- /dev/null +++ b/Spring-DependencyInjection/pom.xml @@ -0,0 +1,30 @@ + + 4.0.0 + + com.spring.core + Spring-DependencyInjection + 0.0.1-SNAPSHOT + jar + + Spring-DependencyInjection + http://maven.apache.org + + + UTF-8 + + + + + org.springframework + spring-context + 4.3.14.RELEASE + + + junit + junit + 3.8.1 + test + + + diff --git a/Spring-DependencyInjection/src/main/java/beans.xml b/Spring-DependencyInjection/src/main/java/beans.xml new file mode 100644 index 0000000000..71de8d3293 --- /dev/null +++ b/Spring-DependencyInjection/src/main/java/beans.xml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Address.java b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Address.java new file mode 100644 index 0000000000..9e6fe29c0c --- /dev/null +++ b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Address.java @@ -0,0 +1,53 @@ +package com.baeldung.spring.core; + +import org.springframework.stereotype.Component; + +@Component +public class Address { + + private String address1; + + private String city; + + private String state; + + private String country; + + public String getAddress1() { + return address1; + } + + public void setAddress1(String address1) { + this.address1 = address1; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + + public String getState() { + return state; + } + + public void setState(String state) { + this.state = state; + } + + public String getCountry() { + return country; + } + + public void setCountry(String country) { + this.country = country; + } + + @Override + public String toString() { + return "Address [address1=" + address1 + ", city=" + city + ", state=" + state + ", country=" + country + "]"; + } + +} diff --git a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Car.java b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Car.java new file mode 100644 index 0000000000..3f940e1e28 --- /dev/null +++ b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Car.java @@ -0,0 +1,30 @@ +package com.baeldung.spring.core; + +public class Car { + + private String carName; + + private String carType; + + public String getCarName() { + return carName; + } + + public void setCarName(String carName) { + this.carName = carName; + } + + public String getCarType() { + return carType; + } + + public void setCarType(String carType) { + this.carType = carType; + } + + @Override + public String toString() { + return "Car [carName=" + carName + ", carType=" + carType + "]"; + } + +} diff --git a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Customer.java b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Customer.java new file mode 100644 index 0000000000..da9ac13021 --- /dev/null +++ b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Customer.java @@ -0,0 +1,61 @@ +package com.baeldung.spring.core; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +@Component +public class Customer { + + @Value("Thomas") + private String customerName; + + @Value("Cust123") + private String customerId; + + private Product product; + + private Address address; + + @Autowired + public Customer(Address address) { + super(); + this.address = address; + } + + public String getCustomerName() { + return customerName; + } + + public void setCustomerName(String customerName) { + this.customerName = customerName; + } + + public String getCustomerId() { + return customerId; + } + + public void setCustomerId(String customerId) { + this.customerId = customerId; + } + + public Product getProduct() { + return product; + } + + @Autowired + public void setProduct(Product product) { + this.product = product; + } + + public Address getAddress() { + return address; + } + + @Override + public String toString() { + return "Customer [customerName=" + customerName + ", customerId=" + customerId + ", product=" + product + + ", address=" + address + "]"; + } + +} diff --git a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Department.java b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Department.java new file mode 100644 index 0000000000..f7da2d560a --- /dev/null +++ b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Department.java @@ -0,0 +1,31 @@ +package com.baeldung.spring.core; + +import org.springframework.stereotype.Component; + +@Component +public class Department { + + private String departmentName; + + private String departmentId; + + public Department(String departmentName, String departmentNumber) { + super(); + this.departmentName = departmentName; + this.departmentId = departmentNumber; + } + + public String getDepartmentName() { + return departmentName; + } + + public String getDepartmentId() { + return departmentId; + } + + @Override + public String toString() { + return "Department [departmentName=" + departmentName + ", departmentNumber=" + departmentId + "]"; + } + +} diff --git a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Employee.java b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Employee.java new file mode 100644 index 0000000000..de450b8e6b --- /dev/null +++ b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Employee.java @@ -0,0 +1,23 @@ +package com.baeldung.spring.core; + +import org.springframework.stereotype.Component; + +@Component +public class Employee { + + private Department department; + + public Employee(Department department) { + super(); + this.department = department; + } + + public Department getDepartment() { + return department; + } + + @Override + public String toString() { + return "Employee [department=" + department + "]"; + } +} diff --git a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Product.java b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Product.java new file mode 100644 index 0000000000..162c9b3429 --- /dev/null +++ b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Product.java @@ -0,0 +1,48 @@ +package com.baeldung.spring.core; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +@Component +public class Product { + + @Value("product1") + private String productName; + + @Value("Pro123") + private String productId; + + @Value("100") + private String productCost; + + public String getProductName() { + return productName; + } + + public void setProductName(String productName) { + this.productName = productName; + } + + public String getProductId() { + return productId; + } + + public void setProductId(String productId) { + this.productId = productId; + } + + public String getProductCost() { + return productCost; + } + + public void setProductCost(String productCost) { + this.productCost = productCost; + } + + @Override + public String toString() { + return "Product [productName=" + productName + ", productId=" + productId + ", productCost=" + productCost + + "]"; + } + +} diff --git a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Student.java b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Student.java new file mode 100644 index 0000000000..f2563f01ec --- /dev/null +++ b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Student.java @@ -0,0 +1,40 @@ +package com.baeldung.spring.core; + +public class Student { + + private String studentName; + + private String studentId; + + private Subject subject; + + public String getStudentName() { + return studentName; + } + + public void setStudentName(String studentName) { + this.studentName = studentName; + } + + public String getStudentId() { + return studentId; + } + + public void setStudentId(String studentId) { + this.studentId = studentId; + } + + public Subject getSubject() { + return subject; + } + + public void setSubject(Subject subject) { + this.subject = subject; + } + + @Override + public String toString() { + return "Student [studentName=" + studentName + ", studentId=" + studentId + ", subject=" + subject + "]"; + } + +} diff --git a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Subject.java b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Subject.java new file mode 100644 index 0000000000..b160bbef18 --- /dev/null +++ b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Subject.java @@ -0,0 +1,30 @@ +package com.baeldung.spring.core; + +public class Subject { + + private String subjectName; + + private String subjectId; + + public String getSubjectName() { + return subjectName; + } + + public void setSubjectName(String subjectName) { + this.subjectName = subjectName; + } + + public String getSubjectId() { + return subjectId; + } + + public void setSubjectId(String subjectId) { + this.subjectId = subjectId; + } + + @Override + public String toString() { + return "Subject [subjectName=" + subjectName + ", subjectId=" + subjectId + "]"; + } + +} diff --git a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/config/AppConfig.java b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/config/AppConfig.java new file mode 100644 index 0000000000..496191d758 --- /dev/null +++ b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/config/AppConfig.java @@ -0,0 +1,30 @@ +package com.baeldung.spring.core.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.baeldung.spring.core.Car; +import com.baeldung.spring.core.Department; +import com.baeldung.spring.core.Employee; + +@Configuration +public class AppConfig { + + @Bean + public Department getDepartment() { + return new Department("Sales", "S01"); + } + + @Bean + public Employee getEmployee() { + return new Employee(getDepartment()); + } + + @Bean + public Car getCar() { + Car car = new Car(); + car.setCarName("Honda"); + car.setCarType("SUV"); + return car; + } +} diff --git a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/test/AnnotationApplicationContextTest.java b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/test/AnnotationApplicationContextTest.java new file mode 100644 index 0000000000..02daf26eb2 --- /dev/null +++ b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/test/AnnotationApplicationContextTest.java @@ -0,0 +1,17 @@ +package com.baeldung.spring.core.test; + +import org.springframework.context.support.AbstractApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import com.baeldung.spring.core.Customer; + +public class AnnotationApplicationContextTest { + + public static void main(String[] args) { + AbstractApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml"); + Customer customer = applicationContext.getBean(Customer.class); + System.out.println("Customer is :: " + customer); + System.out.println("Product is :: " + customer.getProduct()); + applicationContext.close(); + } +} diff --git a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/test/JavaConfigApplicationContextTest.java b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/test/JavaConfigApplicationContextTest.java new file mode 100644 index 0000000000..01b0c67675 --- /dev/null +++ b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/test/JavaConfigApplicationContextTest.java @@ -0,0 +1,23 @@ +package com.baeldung.spring.core.test; + +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.support.AbstractApplicationContext; + +import com.baeldung.spring.core.Car; +import com.baeldung.spring.core.Department; +import com.baeldung.spring.core.Employee; +import com.baeldung.spring.core.config.AppConfig; + +public class JavaConfigApplicationContextTest { + + public static void main(String[] args) { + AbstractApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class); + Department department = applicationContext.getBean(Department.class); + Employee employee = applicationContext.getBean(Employee.class); + System.out.println("Department is :: " + department); + System.out.println("Employee is :: " + employee); + Car car = applicationContext.getBean(Car.class); + System.out.println("Car is :: " + car); + applicationContext.close(); + } +} diff --git a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/test/XMLConfigurationApplicationContextTest.java b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/test/XMLConfigurationApplicationContextTest.java new file mode 100644 index 0000000000..ead4226f1b --- /dev/null +++ b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/test/XMLConfigurationApplicationContextTest.java @@ -0,0 +1,28 @@ +package com.baeldung.spring.core.test; + +import org.springframework.context.support.AbstractApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import com.baeldung.spring.core.Department; +import com.baeldung.spring.core.Employee; +import com.baeldung.spring.core.Student; +import com.baeldung.spring.core.Subject; + +public class XMLConfigurationApplicationContextTest { + + public static void main(String[] args) { + AbstractApplicationContext abstractApplicationContext = new ClassPathXmlApplicationContext("beans.xml"); + // Constructor Injection + Department department = (Department) abstractApplicationContext.getBean("department"); + Employee employee = (Employee) abstractApplicationContext.getBean("employee"); + System.out.println("Department is :: " + department); + System.out.println("Employee is :: " + employee); + + // Setter Injection + Subject subject = (Subject) abstractApplicationContext.getBean("subject"); + Student student = (Student) abstractApplicationContext.getBean("student"); + System.out.println("Subject is :: " + subject); + System.out.println("Student is :: " + student); + abstractApplicationContext.close(); + } +} diff --git a/Spring-DependencyInjection/src/test/java/com/spring/core/Spring_DependencyInjection/AppTest.java b/Spring-DependencyInjection/src/test/java/com/spring/core/Spring_DependencyInjection/AppTest.java new file mode 100644 index 0000000000..5b3c551257 --- /dev/null +++ b/Spring-DependencyInjection/src/test/java/com/spring/core/Spring_DependencyInjection/AppTest.java @@ -0,0 +1,38 @@ +package com.spring.core.Spring_DependencyInjection; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for simple App. + */ +public class AppTest + extends TestCase +{ + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Rigourous Test :-) + */ + public void testApp() + { + assertTrue( true ); + } +}