Added BeanFactory sample in spring-core

This commit is contained in:
Shubham Aggarwal 2016-11-11 14:16:24 +05:30
parent f1d7209198
commit e29e33dbe6
4 changed files with 56 additions and 1 deletions

View File

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

View File

@ -0,0 +1,16 @@
package com.baeldung.beanfactory;
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;
public class BeanFactoryWithClassPathResource {
public void createBeanFactory() {
Resource res = new ClassPathResource("spring-app.xml");
BeanFactory factory = new XmlBeanFactory(res);
Employee emp = (Employee) factory.getBean("employee");
}
}

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,11 @@
<?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>
</beans>