From e29e33dbe63f8602daded4da0d40fcf945d2681f Mon Sep 17 00:00:00 2001 From: Shubham Aggarwal Date: Fri, 11 Nov 2016 14:16:24 +0530 Subject: [PATCH] Added BeanFactory sample in spring-core --- spring-core/.gitignore | 2 +- .../BeanFactoryWithClassPathResource.java | 16 +++++++++++ .../com/baeldung/beanfactory/Employee.java | 28 +++++++++++++++++++ .../com/baeldung/beanfactory/spring-app.xml | 11 ++++++++ 4 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 spring-core/src/main/java/com/baeldung/beanfactory/BeanFactoryWithClassPathResource.java create mode 100644 spring-core/src/main/java/com/baeldung/beanfactory/Employee.java create mode 100644 spring-core/src/main/java/com/baeldung/beanfactory/spring-app.xml diff --git a/spring-core/.gitignore b/spring-core/.gitignore index 6531dfc93f..08259abdaf 100644 --- a/spring-core/.gitignore +++ b/spring-core/.gitignore @@ -5,8 +5,8 @@ RemoteSystemsTempFiles/ bin/ .metadata/ docs/*.autosave -docs/*.autosave .recommenders/ build/ .gradle/ .DS_Store +.idea/ \ No newline at end of file diff --git a/spring-core/src/main/java/com/baeldung/beanfactory/BeanFactoryWithClassPathResource.java b/spring-core/src/main/java/com/baeldung/beanfactory/BeanFactoryWithClassPathResource.java new file mode 100644 index 0000000000..aff425a87b --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/beanfactory/BeanFactoryWithClassPathResource.java @@ -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"); + } +} diff --git a/spring-core/src/main/java/com/baeldung/beanfactory/Employee.java b/spring-core/src/main/java/com/baeldung/beanfactory/Employee.java new file mode 100644 index 0000000000..bd7c7a5dc7 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/beanfactory/Employee.java @@ -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; + } +} diff --git a/spring-core/src/main/java/com/baeldung/beanfactory/spring-app.xml b/spring-core/src/main/java/com/baeldung/beanfactory/spring-app.xml new file mode 100644 index 0000000000..5bdc3e601a --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/beanfactory/spring-app.xml @@ -0,0 +1,11 @@ + + + + + + + + \ No newline at end of file