Merge pull request #820 from sbmaggarwal/master

BAEL-454 BeanFactory sample in spring-core
This commit is contained in:
Marek Lewandowski 2016-11-16 09:49:57 +01:00 committed by GitHub
commit a9bae63774
5 changed files with 73 additions and 7 deletions

View File

@ -5,8 +5,8 @@ RemoteSystemsTempFiles/
bin/
.metadata/
docs/*.autosave
docs/*.autosave
.recommenders/
build/
.gradle/
.DS_Store
.idea/

View File

@ -14,12 +14,6 @@
</description>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
@ -50,6 +44,11 @@
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,26 @@
package com.baeldung.beanfactory;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class BeanFactoryWithClassPathResource {
@Test
public void createBeanFactoryAndCheckEmployeeBean() {
Resource res = new ClassPathResource("spring-app.xml");
BeanFactory factory = new XmlBeanFactory(res);
Employee emp = (Employee) factory.getBean("employee");
assertFalse(factory.isSingleton("employee"));
assertTrue(factory.getBean("employee") instanceof Employee);
assertTrue(factory.isTypeMatch("employee", Employee.class));
assertTrue(factory.getAliases("employee").length > 0);
}
}

View File

@ -0,0 +1,28 @@
package com.baeldung.beanfactory;
public class Employee {
private String name;
private int age;
public Employee(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<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-3.0.xsd">
<bean id="employee" class="com.baeldung.beanfactory.Employee">
<constructor-arg name="name" value="Hello! My name is Java"/>
<constructor-arg name="age" value="18"/>
</bean>
<alias name="employee" alias="empalias" />
</beans>