From 9f87819fc754c941ae44dad149322330a8df8736 Mon Sep 17 00:00:00 2001 From: Ahmed Tawila Date: Mon, 12 Jun 2017 07:22:05 +0200 Subject: [PATCH] Evaluation article: Different Types of Bean Injection in Spring --- .../java/com/baeldung/ConstructorApp.java | 15 ++++++ .../src/main/java/com/baeldung/SetterApp.java | 15 ++++++ .../main/java/com/baeldung/model/College.java | 32 +++++++++++++ .../main/java/com/baeldung/model/Student.java | 46 +++++++++++++++++++ .../main/resources/constructor-context.xml | 17 +++++++ .../src/main/resources/setter-context.xml | 19 ++++++++ 6 files changed, 144 insertions(+) create mode 100644 spring-core/src/main/java/com/baeldung/ConstructorApp.java create mode 100644 spring-core/src/main/java/com/baeldung/SetterApp.java create mode 100644 spring-core/src/main/java/com/baeldung/model/College.java create mode 100644 spring-core/src/main/java/com/baeldung/model/Student.java create mode 100644 spring-core/src/main/resources/constructor-context.xml create mode 100644 spring-core/src/main/resources/setter-context.xml diff --git a/spring-core/src/main/java/com/baeldung/ConstructorApp.java b/spring-core/src/main/java/com/baeldung/ConstructorApp.java new file mode 100644 index 0000000000..89aa55d3b1 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/ConstructorApp.java @@ -0,0 +1,15 @@ +package com.baeldung; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import com.baeldung.model.Student; + +public class ConstructorApp { + public static void main(String[] args) { + ApplicationContext context = new ClassPathXmlApplicationContext("constructor-context.xml"); + + Student student = (Student) context.getBean("student"); + student.introduceMyself(); + } +} diff --git a/spring-core/src/main/java/com/baeldung/SetterApp.java b/spring-core/src/main/java/com/baeldung/SetterApp.java new file mode 100644 index 0000000000..17de7db6dd --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/SetterApp.java @@ -0,0 +1,15 @@ +package com.baeldung; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import com.baeldung.model.Student; + +public class SetterApp { + public static void main(String[] args) { + ApplicationContext context = new ClassPathXmlApplicationContext("setter-context.xml"); + + Student student = (Student) context.getBean("student"); + student.introduceMyself(); + } +} diff --git a/spring-core/src/main/java/com/baeldung/model/College.java b/spring-core/src/main/java/com/baeldung/model/College.java new file mode 100644 index 0000000000..0e55561fe7 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/model/College.java @@ -0,0 +1,32 @@ +package com.baeldung.model; + +public class College { + + private int id; + private String name; + + public College() { + } + + public College(int id, String name) { + this.id = id; + this.name = name; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + +} diff --git a/spring-core/src/main/java/com/baeldung/model/Student.java b/spring-core/src/main/java/com/baeldung/model/Student.java new file mode 100644 index 0000000000..46f01cd32d --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/model/Student.java @@ -0,0 +1,46 @@ +package com.baeldung.model; + +public class Student { + + private int id; + private String name; + private College college; + + public Student() { + } + + public Student(int id, String name, College college) { + this.id = id; + this.name = name; + this.college = college; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public College getCollege() { + return college; + } + + public void setCollege(College college) { + this.college = college; + } + + public void introduceMyself() { + System.out.println("My ID is " + id + " and my name is " + name + ". I am a student at " + college.getName() + " College."); + } + +} diff --git a/spring-core/src/main/resources/constructor-context.xml b/spring-core/src/main/resources/constructor-context.xml new file mode 100644 index 0000000000..67d492a0d6 --- /dev/null +++ b/spring-core/src/main/resources/constructor-context.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + diff --git a/spring-core/src/main/resources/setter-context.xml b/spring-core/src/main/resources/setter-context.xml new file mode 100644 index 0000000000..e97bfb4b11 --- /dev/null +++ b/spring-core/src/main/resources/setter-context.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + \ No newline at end of file