From e29e33dbe63f8602daded4da0d40fcf945d2681f Mon Sep 17 00:00:00 2001 From: Shubham Aggarwal Date: Fri, 11 Nov 2016 14:16:24 +0530 Subject: [PATCH 1/5] 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 From e425d8bf01a52b013c25172f13cab43e836bb323 Mon Sep 17 00:00:00 2001 From: Shubham Aggarwal Date: Fri, 11 Nov 2016 14:16:24 +0530 Subject: [PATCH 2/5] 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 From 40841f93e5a1562c274e9a7792b05ecc610a4f06 Mon Sep 17 00:00:00 2001 From: Shubham Aggarwal Date: Mon, 14 Nov 2016 20:22:24 +0530 Subject: [PATCH 3/5] Added Test for BeanFactory methods --- spring-core/pom.xml | 5 +++++ .../BeanFactoryWithClassPathResource.java | 19 +++++++++++++++++-- .../com/baeldung/beanfactory/spring-app.xml | 2 ++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/spring-core/pom.xml b/spring-core/pom.xml index 9b94ba7b35..9b6addf0f4 100644 --- a/spring-core/pom.xml +++ b/spring-core/pom.xml @@ -50,6 +50,11 @@ javax.inject 1 + + junit + junit + 4.12 + diff --git a/spring-core/src/main/java/com/baeldung/beanfactory/BeanFactoryWithClassPathResource.java b/spring-core/src/main/java/com/baeldung/beanfactory/BeanFactoryWithClassPathResource.java index aff425a87b..bb9309f394 100644 --- a/spring-core/src/main/java/com/baeldung/beanfactory/BeanFactoryWithClassPathResource.java +++ b/spring-core/src/main/java/com/baeldung/beanfactory/BeanFactoryWithClassPathResource.java @@ -1,16 +1,31 @@ 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 { + Resource res = new ClassPathResource("spring-app.xml"); + BeanFactory factory = new XmlBeanFactory(res); + + @Test public void createBeanFactory() { - 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)); + + //we have empalias as an alias for employee + assertTrue(factory.getAliases("employee").length > 0); } } 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 index 5bdc3e601a..7b3d4f29ed 100644 --- a/spring-core/src/main/java/com/baeldung/beanfactory/spring-app.xml +++ b/spring-core/src/main/java/com/baeldung/beanfactory/spring-app.xml @@ -8,4 +8,6 @@ + + \ No newline at end of file From 89ebafc9a6c92d6df5beb6dcfcf312f85266d038 Mon Sep 17 00:00:00 2001 From: Shubham Aggarwal Date: Wed, 16 Nov 2016 08:29:47 +0530 Subject: [PATCH 4/5] Added assertions. --- .../beanfactory/BeanFactoryWithClassPathResource.java | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/spring-core/src/main/java/com/baeldung/beanfactory/BeanFactoryWithClassPathResource.java b/spring-core/src/main/java/com/baeldung/beanfactory/BeanFactoryWithClassPathResource.java index bb9309f394..a3bdbba848 100644 --- a/spring-core/src/main/java/com/baeldung/beanfactory/BeanFactoryWithClassPathResource.java +++ b/spring-core/src/main/java/com/baeldung/beanfactory/BeanFactoryWithClassPathResource.java @@ -11,21 +11,16 @@ import static org.junit.Assert.assertTrue; public class BeanFactoryWithClassPathResource { - Resource res = new ClassPathResource("spring-app.xml"); - BeanFactory factory = new XmlBeanFactory(res); - @Test public void createBeanFactory() { + 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)); - - //we have empalias as an alias for employee assertTrue(factory.getAliases("employee").length > 0); } } From 5e0f94f883fb5288c3eb35035c08ed2986954b08 Mon Sep 17 00:00:00 2001 From: Shubham Aggarwal Date: Wed, 16 Nov 2016 14:09:46 +0530 Subject: [PATCH 5/5] Updated junit dependency in pom, named test method meaningful --- spring-core/pom.xml | 6 ------ .../beanfactory/BeanFactoryWithClassPathResource.java | 2 +- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/spring-core/pom.xml b/spring-core/pom.xml index 9b6addf0f4..a2e306a61f 100644 --- a/spring-core/pom.xml +++ b/spring-core/pom.xml @@ -14,12 +14,6 @@ - - junit - junit - 4.11 - test - org.mockito mockito-all diff --git a/spring-core/src/main/java/com/baeldung/beanfactory/BeanFactoryWithClassPathResource.java b/spring-core/src/main/java/com/baeldung/beanfactory/BeanFactoryWithClassPathResource.java index a3bdbba848..3a7682641e 100644 --- a/spring-core/src/main/java/com/baeldung/beanfactory/BeanFactoryWithClassPathResource.java +++ b/spring-core/src/main/java/com/baeldung/beanfactory/BeanFactoryWithClassPathResource.java @@ -12,7 +12,7 @@ import static org.junit.Assert.assertTrue; public class BeanFactoryWithClassPathResource { @Test - public void createBeanFactory() { + public void createBeanFactoryAndCheckEmployeeBean() { Resource res = new ClassPathResource("spring-app.xml"); BeanFactory factory = new XmlBeanFactory(res);