Spring-dependency injection
This commit is contained in:
parent
8755eb18f7
commit
0dab47c76d
|
@ -0,0 +1,30 @@
|
|||
<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>com.spring.core</groupId>
|
||||
<artifactId>Spring-DependencyInjection</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>Spring-DependencyInjection</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>4.3.14.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>3.8.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
|
@ -0,0 +1,31 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
|
||||
|
||||
<context:annotation-config />
|
||||
<context:component-scan base-package="com.baeldung.spring.core" />
|
||||
|
||||
<bean id="department" class="com.baeldung.spring.core.Department">
|
||||
<constructor-arg name="departmentName" value="Sales" type="String" />
|
||||
<constructor-arg name="departmentNumber" value="S01" type="String" />
|
||||
</bean>
|
||||
|
||||
<bean id="employee" class="com.baeldung.spring.core.Employee">
|
||||
<constructor-arg name="department" ref="department" />
|
||||
</bean>
|
||||
|
||||
<bean id="subject" class="com.baeldung.spring.core.Subject">
|
||||
<property name="subjectName" value="Maths" />
|
||||
<property name="subjectId" value="M1" />
|
||||
</bean>
|
||||
|
||||
<bean id="student" class="com.baeldung.spring.core.Student">
|
||||
<property name="studentName" value="student1" />
|
||||
<property name="studentId" value="123" />
|
||||
<property name="subject" ref="subject" />
|
||||
</bean>
|
||||
|
||||
</beans>
|
|
@ -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 + "]";
|
||||
}
|
||||
|
||||
}
|
|
@ -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 + "]";
|
||||
}
|
||||
|
||||
}
|
|
@ -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 + "]";
|
||||
}
|
||||
|
||||
}
|
|
@ -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 + "]";
|
||||
}
|
||||
|
||||
}
|
|
@ -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 + "]";
|
||||
}
|
||||
}
|
|
@ -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
|
||||
+ "]";
|
||||
}
|
||||
|
||||
}
|
|
@ -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 + "]";
|
||||
}
|
||||
|
||||
}
|
|
@ -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 + "]";
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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 );
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue