Skip to content

Commit 0dab47c

Browse files
committed
Spring-dependency injection
1 parent 8755eb1 commit 0dab47c

15 files changed

Lines changed: 513 additions & 0 deletions

File tree

‎Spring-DependencyInjection/pom.xml‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.spring.core</groupId>
6+
<artifactId>Spring-DependencyInjection</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>Spring-DependencyInjection</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.springframework</groupId>
20+
<artifactId>spring-context</artifactId>
21+
<version>4.3.14.RELEASE</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>junit</groupId>
25+
<artifactId>junit</artifactId>
26+
<version>3.8.1</version>
27+
<scope>test</scope>
28+
</dependency>
29+
</dependencies>
30+
</project>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
4+
xsi:schemaLocation="
5+
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
6+
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
7+
8+
<context:annotation-config />
9+
<context:component-scan base-package="com.baeldung.spring.core" />
10+
11+
<bean id="department" class="com.baeldung.spring.core.Department">
12+
<constructor-arg name="departmentName" value="Sales" type="String" />
13+
<constructor-arg name="departmentNumber" value="S01" type="String" />
14+
</bean>
15+
16+
<bean id="employee" class="com.baeldung.spring.core.Employee">
17+
<constructor-arg name="department" ref="department" />
18+
</bean>
19+
20+
<bean id="subject" class="com.baeldung.spring.core.Subject">
21+
<property name="subjectName" value="Maths" />
22+
<property name="subjectId" value="M1" />
23+
</bean>
24+
25+
<bean id="student" class="com.baeldung.spring.core.Student">
26+
<property name="studentName" value="student1" />
27+
<property name="studentId" value="123" />
28+
<property name="subject" ref="subject" />
29+
</bean>
30+
31+
</beans>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.baeldung.spring.core;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
@Component
6+
public class Address {
7+
8+
private String address1;
9+
10+
private String city;
11+
12+
private String state;
13+
14+
private String country;
15+
16+
public String getAddress1() {
17+
return address1;
18+
}
19+
20+
public void setAddress1(String address1) {
21+
this.address1 = address1;
22+
}
23+
24+
public String getCity() {
25+
return city;
26+
}
27+
28+
public void setCity(String city) {
29+
this.city = city;
30+
}
31+
32+
public String getState() {
33+
return state;
34+
}
35+
36+
public void setState(String state) {
37+
this.state = state;
38+
}
39+
40+
public String getCountry() {
41+
return country;
42+
}
43+
44+
public void setCountry(String country) {
45+
this.country = country;
46+
}
47+
48+
@Override
49+
public String toString() {
50+
return "Address [address1=" + address1 + ", city=" + city + ", state=" + state + ", country=" + country + "]";
51+
}
52+
53+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.spring.core;
2+
3+
public class Car {
4+
5+
private String carName;
6+
7+
private String carType;
8+
9+
public String getCarName() {
10+
return carName;
11+
}
12+
13+
public void setCarName(String carName) {
14+
this.carName = carName;
15+
}
16+
17+
public String getCarType() {
18+
return carType;
19+
}
20+
21+
public void setCarType(String carType) {
22+
this.carType = carType;
23+
}
24+
25+
@Override
26+
public String toString() {
27+
return "Car [carName=" + carName + ", carType=" + carType + "]";
28+
}
29+
30+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.baeldung.spring.core;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.beans.factory.annotation.Value;
5+
import org.springframework.stereotype.Component;
6+
7+
@Component
8+
public class Customer {
9+
10+
@Value("Thomas")
11+
private String customerName;
12+
13+
@Value("Cust123")
14+
private String customerId;
15+
16+
private Product product;
17+
18+
private Address address;
19+
20+
@Autowired
21+
public Customer(Address address) {
22+
super();
23+
this.address = address;
24+
}
25+
26+
public String getCustomerName() {
27+
return customerName;
28+
}
29+
30+
public void setCustomerName(String customerName) {
31+
this.customerName = customerName;
32+
}
33+
34+
public String getCustomerId() {
35+
return customerId;
36+
}
37+
38+
public void setCustomerId(String customerId) {
39+
this.customerId = customerId;
40+
}
41+
42+
public Product getProduct() {
43+
return product;
44+
}
45+
46+
@Autowired
47+
public void setProduct(Product product) {
48+
this.product = product;
49+
}
50+
51+
public Address getAddress() {
52+
return address;
53+
}
54+
55+
@Override
56+
public String toString() {
57+
return "Customer [customerName=" + customerName + ", customerId=" + customerId + ", product=" + product
58+
+ ", address=" + address + "]";
59+
}
60+
61+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.spring.core;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
@Component
6+
public class Department {
7+
8+
private String departmentName;
9+
10+
private String departmentId;
11+
12+
public Department(String departmentName, String departmentNumber) {
13+
super();
14+
this.departmentName = departmentName;
15+
this.departmentId = departmentNumber;
16+
}
17+
18+
public String getDepartmentName() {
19+
return departmentName;
20+
}
21+
22+
public String getDepartmentId() {
23+
return departmentId;
24+
}
25+
26+
@Override
27+
public String toString() {
28+
return "Department [departmentName=" + departmentName + ", departmentNumber=" + departmentId + "]";
29+
}
30+
31+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.spring.core;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
@Component
6+
public class Employee {
7+
8+
private Department department;
9+
10+
public Employee(Department department) {
11+
super();
12+
this.department = department;
13+
}
14+
15+
public Department getDepartment() {
16+
return department;
17+
}
18+
19+
@Override
20+
public String toString() {
21+
return "Employee [department=" + department + "]";
22+
}
23+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.spring.core;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.stereotype.Component;
5+
6+
@Component
7+
public class Product {
8+
9+
@Value("product1")
10+
private String productName;
11+
12+
@Value("Pro123")
13+
private String productId;
14+
15+
@Value("100")
16+
private String productCost;
17+
18+
public String getProductName() {
19+
return productName;
20+
}
21+
22+
public void setProductName(String productName) {
23+
this.productName = productName;
24+
}
25+
26+
public String getProductId() {
27+
return productId;
28+
}
29+
30+
public void setProductId(String productId) {
31+
this.productId = productId;
32+
}
33+
34+
public String getProductCost() {
35+
return productCost;
36+
}
37+
38+
public void setProductCost(String productCost) {
39+
this.productCost = productCost;
40+
}
41+
42+
@Override
43+
public String toString() {
44+
return "Product [productName=" + productName + ", productId=" + productId + ", productCost=" + productCost
45+
+ "]";
46+
}
47+
48+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.spring.core;
2+
3+
public class Student {
4+
5+
private String studentName;
6+
7+
private String studentId;
8+
9+
private Subject subject;
10+
11+
public String getStudentName() {
12+
return studentName;
13+
}
14+
15+
public void setStudentName(String studentName) {
16+
this.studentName = studentName;
17+
}
18+
19+
public String getStudentId() {
20+
return studentId;
21+
}
22+
23+
public void setStudentId(String studentId) {
24+
this.studentId = studentId;
25+
}
26+
27+
public Subject getSubject() {
28+
return subject;
29+
}
30+
31+
public void setSubject(Subject subject) {
32+
this.subject = subject;
33+
}
34+
35+
@Override
36+
public String toString() {
37+
return "Student [studentName=" + studentName + ", studentId=" + studentId + ", subject=" + subject + "]";
38+
}
39+
40+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.spring.core;
2+
3+
public class Subject {
4+
5+
private String subjectName;
6+
7+
private String subjectId;
8+
9+
public String getSubjectName() {
10+
return subjectName;
11+
}
12+
13+
public void setSubjectName(String subjectName) {
14+
this.subjectName = subjectName;
15+
}
16+
17+
public String getSubjectId() {
18+
return subjectId;
19+
}
20+
21+
public void setSubjectId(String subjectId) {
22+
this.subjectId = subjectId;
23+
}
24+
25+
@Override
26+
public String toString() {
27+
return "Subject [subjectName=" + subjectName + ", subjectId=" + subjectId + "]";
28+
}
29+
30+
}

0 commit comments

Comments
 (0)