Skip to content

Commit 2ec5f75

Browse files
committed
Add new code samples
- Inversion of control - Constructor injection (using xml and annotations) - Setter Injection (Using xml and annotations) - Autowired annotation
1 parent 7846bca commit 2ec5f75

14 files changed

+300
-0
lines changed

code-samples/spring.core/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target/

code-samples/spring.core/pom.xml

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com</groupId>
7+
<artifactId>spring.core</artifactId>
8+
<version>1.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>spring.core</name>
12+
<url>http://maven.apache.org</url>
13+
14+
<properties>
15+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16+
<maven.compiler.target>1.8</maven.compiler.target>
17+
<maven.compiler.source>1.8</maven.compiler.source>
18+
</properties>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>junit</groupId>
23+
<artifactId>junit</artifactId>
24+
<version>3.8.1</version>
25+
<scope>test</scope>
26+
</dependency>
27+
28+
<dependency>
29+
<groupId>org.springframework</groupId>
30+
<artifactId>spring-context</artifactId>
31+
<version>5.2.16.RELEASE</version>
32+
</dependency>
33+
</dependencies>
34+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<beans xmlns="http://www.springframework.org/schema/beans"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns:context="http://www.springframework.org/schema/context"
6+
xsi:schemaLocation="http://www.springframework.org/schema/beans
7+
http://www.springframework.org/schema/beans/spring-beans.xsd
8+
http://www.springframework.org/schema/context
9+
http://www.springframework.org/schema/context/spring-context.xsd">
10+
11+
<!-- Define beans using annotations -->
12+
<context:component-scan
13+
base-package="com.example"></context:component-scan>
14+
15+
<!-- Inversion of control -->
16+
<bean id="accountantXml" class="com.example.Accountant"></bean>
17+
<bean id="secuirtyGuardXml" class="com.example.SecurityGuard"></bean>
18+
19+
<!-- Constructor Injection -->
20+
<bean id="accountantDepartmentXml"
21+
class="com.example.AccountingDepartment"></bean>
22+
23+
<bean id="accountantConstructorXml" class="com.example.Accountant">
24+
<constructor-arg index="0" value="Ahmed Osama Tantawy" />
25+
<constructor-arg index="1"
26+
ref="accountantDepartmentXml" />
27+
</bean>
28+
29+
<!-- Setter Injection -->
30+
31+
<bean id="securityDepartmentXml"
32+
class="com.example.SecurityDepartment"></bean>
33+
34+
<bean id="securitySetterXml" class="com.example.SecurityGuard">
35+
<property name="employeeName" value="Ahmed Osama Tantawy"></property>
36+
<property name="department" ref="securityDepartmentXml"></property>
37+
</bean>
38+
39+
40+
</beans>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.example;
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 // For defining bean using annotations
8+
public class Accountant implements Employee {
9+
@Value("osos")
10+
private String employeeName;
11+
12+
@Autowired
13+
private Department department;
14+
15+
public Accountant() {
16+
}
17+
18+
public Accountant(String employeeName, Department department) {
19+
this.employeeName = employeeName;
20+
this.department = department;
21+
}
22+
23+
@Override
24+
public String getWorkingHours() {
25+
return "Accountant: work from 9 => 5";
26+
}
27+
28+
@Override
29+
public String toString() {
30+
return "Accountant [employeeName=" + employeeName + ", department=" + department.getDepartmentName() + "]";
31+
}
32+
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.example;
2+
3+
import org.springframework.context.annotation.Primary;
4+
import org.springframework.stereotype.Component;
5+
6+
@Component
7+
@Primary
8+
public class AccountingDepartment implements Department {
9+
10+
@Override
11+
public String getDepartmentName() {
12+
return "Accounting Department";
13+
}
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.example;
2+
3+
public interface Department {
4+
String getDepartmentName();
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.example;
2+
3+
public interface Employee {
4+
String getWorkingHours();
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.example;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
@Component
6+
public class SecurityDepartment implements Department {
7+
8+
@Override
9+
public String getDepartmentName() {
10+
return "Security Department";
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.example;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
@Component // for defining beans using annotations
6+
public class SecurityGuard implements Employee {
7+
private String employeeName;
8+
private Department department;
9+
10+
public String getEmployeeName() {
11+
return employeeName;
12+
}
13+
14+
public void setEmployeeName(String employeeName) {
15+
this.employeeName = employeeName;
16+
}
17+
18+
public Department getDepartment() {
19+
return department;
20+
}
21+
22+
public void setDepartment(Department department) {
23+
this.department = department;
24+
}
25+
26+
@Override
27+
public String getWorkingHours() {
28+
return "Security guard: 12am => 7am";
29+
30+
}
31+
32+
@Override
33+
public String toString() {
34+
return "SecurityGuard [employeeName=" + employeeName + ", department=" + department.getDepartmentName() + "]";
35+
}
36+
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.spring;
2+
3+
import org.springframework.context.ApplicationContext;
4+
import org.springframework.context.support.ClassPathXmlApplicationContext;
5+
6+
import com.example.Employee;
7+
8+
public class AnnotationUsingAutowired {
9+
10+
public static void main(String[] args) {
11+
ApplicationContext appCtx = new ClassPathXmlApplicationContext("applicationContext.xml");
12+
13+
Employee emp = appCtx.getBean("accountant", Employee.class);
14+
System.out.println(emp);
15+
16+
((ClassPathXmlApplicationContext) appCtx).close();
17+
18+
}
19+
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.spring;
2+
3+
import org.springframework.context.ApplicationContext;
4+
import org.springframework.context.support.ClassPathXmlApplicationContext;
5+
6+
import com.example.Employee;
7+
8+
public class ConstructorInjection {
9+
10+
public static void main(String[] args) {
11+
ApplicationContext appCtx = new ClassPathXmlApplicationContext("applicationContext.xml");
12+
13+
Employee emp = appCtx.getBean("accountantConstructorXml", Employee.class);
14+
15+
System.out.println(emp);
16+
17+
((ClassPathXmlApplicationContext) appCtx).close();
18+
}
19+
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.spring;
2+
3+
import org.springframework.context.ApplicationContext;
4+
import org.springframework.context.support.ClassPathXmlApplicationContext;
5+
6+
import com.example.Employee;
7+
8+
public class InversionOfControl {
9+
10+
public static void main(String[] args) {
11+
12+
ApplicationContext appCtx = new ClassPathXmlApplicationContext("applicationContext.xml");
13+
14+
Employee emp = appCtx.getBean("accountantXml", Employee.class);
15+
// Employee emp = appCtx.getBean("securityGuardXml", Exmployee.class);
16+
System.out.println(emp.getWorkingHours());
17+
18+
((ClassPathXmlApplicationContext) appCtx).close();
19+
}
20+
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.spring;
2+
3+
import org.springframework.context.ApplicationContext;
4+
import org.springframework.context.support.ClassPathXmlApplicationContext;
5+
6+
import com.example.Employee;
7+
8+
public class SetterInjection {
9+
10+
public static void main(String[] args) {
11+
ApplicationContext appCtx = new ClassPathXmlApplicationContext("applicationContext.xml");
12+
13+
Employee emp = appCtx.getBean("securitySetterXml", Employee.class);
14+
System.out.println(emp);
15+
((ClassPathXmlApplicationContext) appCtx).close();
16+
}
17+
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.spring.core;
2+
3+
import junit.framework.Test;
4+
import junit.framework.TestCase;
5+
import junit.framework.TestSuite;
6+
7+
/**
8+
* Unit test for simple App.
9+
*/
10+
public class AppTest
11+
extends TestCase
12+
{
13+
/**
14+
* Create the test case
15+
*
16+
* @param testName name of the test case
17+
*/
18+
public AppTest( String testName )
19+
{
20+
super( testName );
21+
}
22+
23+
/**
24+
* @return the suite of tests being tested
25+
*/
26+
public static Test suite()
27+
{
28+
return new TestSuite( AppTest.class );
29+
}
30+
31+
/**
32+
* Rigourous Test :-)
33+
*/
34+
public void testApp()
35+
{
36+
assertTrue( true );
37+
}
38+
}

0 commit comments

Comments
 (0)