Skip to content

Commit e425d8b

Browse files
committed
Added BeanFactory sample in spring-core
1 parent ec13234 commit e425d8b

4 files changed

Lines changed: 56 additions & 1 deletion

File tree

‎spring-core/.gitignore‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ RemoteSystemsTempFiles/
55
bin/
66
.metadata/
77
docs/*.autosave
8-
docs/*.autosave
98
.recommenders/
109
build/
1110
.gradle/
1211
.DS_Store
12+
.idea/
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.beanfactory;
2+
3+
import org.springframework.beans.factory.BeanFactory;
4+
import org.springframework.beans.factory.xml.XmlBeanFactory;
5+
import org.springframework.core.io.ClassPathResource;
6+
import org.springframework.core.io.Resource;
7+
8+
public class BeanFactoryWithClassPathResource {
9+
10+
public void createBeanFactory() {
11+
12+
Resource res = new ClassPathResource("spring-app.xml");
13+
BeanFactory factory = new XmlBeanFactory(res);
14+
Employee emp = (Employee) factory.getBean("employee");
15+
}
16+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.beanfactory;
2+
3+
public class Employee {
4+
5+
private String name;
6+
private int age;
7+
8+
public Employee(String name, int age) {
9+
this.name = name;
10+
this.age = age;
11+
}
12+
13+
public String getName() {
14+
return name;
15+
}
16+
17+
public void setName(String name) {
18+
this.name = name;
19+
}
20+
21+
public int getAge() {
22+
return age;
23+
}
24+
25+
public void setAge(int age) {
26+
this.age = age;
27+
}
28+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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"
4+
xsi:schemaLocation="http://www.springframework.org/schema/beans
5+
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
6+
7+
<bean id="employee" class="com.baeldung.beanfactory.Employee">
8+
<constructor-arg name="name" value="Hello! My name is Java"/>
9+
<constructor-arg name="age" value="18"/>
10+
</bean>
11+
</beans>

0 commit comments

Comments
 (0)