Skip to content

Commit a9bae63

Browse files
authored
Merge pull request eugenp#820 from sbmaggarwal/master
BAEL-454 BeanFactory sample in spring-core
2 parents 4681d3d + 5e0f94f commit a9bae63

5 files changed

Lines changed: 73 additions & 7 deletions

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/

‎spring-core/pom.xml‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@
1414
</description>
1515

1616
<dependencies>
17-
<dependency>
18-
<groupId>junit</groupId>
19-
<artifactId>junit</artifactId>
20-
<version>4.11</version>
21-
<scope>test</scope>
22-
</dependency>
2317
<dependency>
2418
<groupId>org.mockito</groupId>
2519
<artifactId>mockito-all</artifactId>
@@ -50,6 +44,11 @@
5044
<artifactId>javax.inject</artifactId>
5145
<version>1</version>
5246
</dependency>
47+
<dependency>
48+
<groupId>junit</groupId>
49+
<artifactId>junit</artifactId>
50+
<version>4.12</version>
51+
</dependency>
5352
</dependencies>
5453

5554
<build>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.beanfactory;
2+
3+
import org.junit.Test;
4+
import org.springframework.beans.factory.BeanFactory;
5+
import org.springframework.beans.factory.xml.XmlBeanFactory;
6+
import org.springframework.core.io.ClassPathResource;
7+
import org.springframework.core.io.Resource;
8+
9+
import static org.junit.Assert.assertFalse;
10+
import static org.junit.Assert.assertTrue;
11+
12+
public class BeanFactoryWithClassPathResource {
13+
14+
@Test
15+
public void createBeanFactoryAndCheckEmployeeBean() {
16+
17+
Resource res = new ClassPathResource("spring-app.xml");
18+
BeanFactory factory = new XmlBeanFactory(res);
19+
Employee emp = (Employee) factory.getBean("employee");
20+
21+
assertFalse(factory.isSingleton("employee"));
22+
assertTrue(factory.getBean("employee") instanceof Employee);
23+
assertTrue(factory.isTypeMatch("employee", Employee.class));
24+
assertTrue(factory.getAliases("employee").length > 0);
25+
}
26+
}
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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
12+
<alias name="employee" alias="empalias" />
13+
</beans>

0 commit comments

Comments
 (0)