Evaluation article: Different Types of Bean Injection in Spring
This commit is contained in:
parent
92b67adc12
commit
9f87819fc7
15
spring-core/src/main/java/com/baeldung/ConstructorApp.java
Normal file
15
spring-core/src/main/java/com/baeldung/ConstructorApp.java
Normal 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();
|
||||
}
|
||||
}
|
15
spring-core/src/main/java/com/baeldung/SetterApp.java
Normal file
15
spring-core/src/main/java/com/baeldung/SetterApp.java
Normal 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();
|
||||
}
|
||||
}
|
32
spring-core/src/main/java/com/baeldung/model/College.java
Normal file
32
spring-core/src/main/java/com/baeldung/model/College.java
Normal 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;
|
||||
}
|
||||
|
||||
}
|
46
spring-core/src/main/java/com/baeldung/model/Student.java
Normal file
46
spring-core/src/main/java/com/baeldung/model/Student.java
Normal 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.");
|
||||
}
|
||||
|
||||
}
|
17
spring-core/src/main/resources/constructor-context.xml
Normal file
17
spring-core/src/main/resources/constructor-context.xml
Normal 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>
|
19
spring-core/src/main/resources/setter-context.xml
Normal file
19
spring-core/src/main/resources/setter-context.xml
Normal 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>
|
Loading…
x
Reference in New Issue
Block a user