Evaluation article: Different Types of Bean Injection in Spring

This commit is contained in:
Ahmed Tawila 2017-06-12 07:22:05 +02:00
parent 92b67adc12
commit 9f87819fc7
6 changed files with 144 additions and 0 deletions

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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;
}
}

View File

@ -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.");
}
}

View File

@ -0,0 +1,17 @@
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student" class="com.baeldung.model.Student">
<constructor-arg value="1" />
<constructor-arg value="John" />
<constructor-arg ref="college" />
</bean>
<bean id="college" class="com.baeldung.model.College">
<constructor-arg value="1" />
<constructor-arg value="New York" />
</bean>
</beans>

View File

@ -0,0 +1,19 @@
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="college" class="com.baeldung.model.College">
<property name="id" value="2" />
<property name="name" value="Pittsburgh" />
</bean>
<bean id="student" class="com.baeldung.model.Student">
<property name="id" value="2" />
<property name="name" value="Tom" />
<property name="college">
<ref bean="college" />
</property>
</bean>
</beans>