From c1e0b884de333e7f1623f1b04c78830f61d57dc5 Mon Sep 17 00:00:00 2001 From: mherbaghinyan Date: Tue, 24 Apr 2018 22:28:20 +0400 Subject: [PATCH 1/4] aware --- .../src/main/java/com/baeldung/aware/AwareExample.java | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 spring-core/src/main/java/com/baeldung/aware/AwareExample.java diff --git a/spring-core/src/main/java/com/baeldung/aware/AwareExample.java b/spring-core/src/main/java/com/baeldung/aware/AwareExample.java new file mode 100644 index 0000000000..90e2ccf074 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/aware/AwareExample.java @@ -0,0 +1,7 @@ +package com.baeldung.aware; + +/** + * Created by Gebruiker on 4/24/2018. + */ +public class AwareExample { +} From 4b91a76d8f80db511e9951e7a13eba5181edd3f4 Mon Sep 17 00:00:00 2001 From: mherbaghinyan Date: Wed, 25 Apr 2018 22:13:50 +0400 Subject: [PATCH 2/4] BeanNameAware and BeanFactoryAware --- spring-core/pom.xml | 5 +++++ .../java/com/baeldung/aware/AwareExample.java | 4 ++++ .../main/java/com/baeldung/aware/Config.java | 7 +++++++ .../com/baeldung/aware/MyBeanFactory.java | 20 +++++++++++++++++++ .../java/com/baeldung/aware/MyBeanName.java | 14 +++++++++++++ 5 files changed, 50 insertions(+) create mode 100644 spring-core/src/main/java/com/baeldung/aware/Config.java create mode 100644 spring-core/src/main/java/com/baeldung/aware/MyBeanFactory.java create mode 100644 spring-core/src/main/java/com/baeldung/aware/MyBeanName.java diff --git a/spring-core/pom.xml b/spring-core/pom.xml index ae1b93a403..b80e78162d 100644 --- a/spring-core/pom.xml +++ b/spring-core/pom.xml @@ -74,6 +74,11 @@ commons-io ${commons.io.version} + + org.springframework + spring-beans + ${spring.version} + diff --git a/spring-core/src/main/java/com/baeldung/aware/AwareExample.java b/spring-core/src/main/java/com/baeldung/aware/AwareExample.java index 90e2ccf074..bbb57f1464 100644 --- a/spring-core/src/main/java/com/baeldung/aware/AwareExample.java +++ b/spring-core/src/main/java/com/baeldung/aware/AwareExample.java @@ -4,4 +4,8 @@ package com.baeldung.aware; * Created by Gebruiker on 4/24/2018. */ public class AwareExample { + + public static void main(String[] args) { + + } } diff --git a/spring-core/src/main/java/com/baeldung/aware/Config.java b/spring-core/src/main/java/com/baeldung/aware/Config.java new file mode 100644 index 0000000000..fc40bdf61b --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/aware/Config.java @@ -0,0 +1,7 @@ +package com.baeldung.aware; + +import org.springframework.context.annotation.Configuration; + +@Configuration +public class Config { +} diff --git a/spring-core/src/main/java/com/baeldung/aware/MyBeanFactory.java b/spring-core/src/main/java/com/baeldung/aware/MyBeanFactory.java new file mode 100644 index 0000000000..047e1372be --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/aware/MyBeanFactory.java @@ -0,0 +1,20 @@ +package com.baeldung.aware; + +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.BeanFactory; +import org.springframework.beans.factory.BeanFactoryAware; + +/** + * Created by Gebruiker on 4/25/2018. + */ +public class MyBeanFactory implements BeanFactoryAware { + + private BeanFactory beanFactory; + + @Override + public void setBeanFactory(BeanFactory beanFactory) throws BeansException { + + this.beanFactory = beanFactory; + System.out.println(beanFactory); + } +} diff --git a/spring-core/src/main/java/com/baeldung/aware/MyBeanName.java b/spring-core/src/main/java/com/baeldung/aware/MyBeanName.java new file mode 100644 index 0000000000..91a3fc9a16 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/aware/MyBeanName.java @@ -0,0 +1,14 @@ +package com.baeldung.aware; + +import org.springframework.beans.factory.BeanNameAware; + +/** + * Created by Gebruiker on 4/25/2018. + */ +public class MyBeanName implements BeanNameAware { + + @Override + public void setBeanName(String beanName) { + System.out.println(beanName); + } +} From 5dc1b18c29899a29e5698c309b0c86efc9a15616 Mon Sep 17 00:00:00 2001 From: mherbaghinyan Date: Sat, 28 Apr 2018 12:02:06 +0400 Subject: [PATCH 3/4] BeanNameAware test the result --- .../src/main/java/com/baeldung/aware/AwareExample.java | 6 ++++++ spring-core/src/main/java/com/baeldung/aware/Config.java | 6 ++++++ .../src/main/java/com/baeldung/aware/MyBeanFactory.java | 2 ++ .../src/main/java/com/baeldung/aware/MyBeanName.java | 3 --- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/spring-core/src/main/java/com/baeldung/aware/AwareExample.java b/spring-core/src/main/java/com/baeldung/aware/AwareExample.java index bbb57f1464..20770c7f08 100644 --- a/spring-core/src/main/java/com/baeldung/aware/AwareExample.java +++ b/spring-core/src/main/java/com/baeldung/aware/AwareExample.java @@ -1,5 +1,7 @@ package com.baeldung.aware; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + /** * Created by Gebruiker on 4/24/2018. */ @@ -7,5 +9,9 @@ public class AwareExample { public static void main(String[] args) { + AnnotationConfigApplicationContext context + = new AnnotationConfigApplicationContext(Config.class); + + MyBeanName myBeanName = context.getBean(MyBeanName.class); } } diff --git a/spring-core/src/main/java/com/baeldung/aware/Config.java b/spring-core/src/main/java/com/baeldung/aware/Config.java index fc40bdf61b..547d02a60b 100644 --- a/spring-core/src/main/java/com/baeldung/aware/Config.java +++ b/spring-core/src/main/java/com/baeldung/aware/Config.java @@ -1,7 +1,13 @@ package com.baeldung.aware; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class Config { + + @Bean(name = "myCustomBeanName") + public MyBeanName getMyBeanName() { + return new MyBeanName(); + } } diff --git a/spring-core/src/main/java/com/baeldung/aware/MyBeanFactory.java b/spring-core/src/main/java/com/baeldung/aware/MyBeanFactory.java index 047e1372be..8bc5de9e45 100644 --- a/spring-core/src/main/java/com/baeldung/aware/MyBeanFactory.java +++ b/spring-core/src/main/java/com/baeldung/aware/MyBeanFactory.java @@ -17,4 +17,6 @@ public class MyBeanFactory implements BeanFactoryAware { this.beanFactory = beanFactory; System.out.println(beanFactory); } + + } diff --git a/spring-core/src/main/java/com/baeldung/aware/MyBeanName.java b/spring-core/src/main/java/com/baeldung/aware/MyBeanName.java index 91a3fc9a16..cfd6a5765d 100644 --- a/spring-core/src/main/java/com/baeldung/aware/MyBeanName.java +++ b/spring-core/src/main/java/com/baeldung/aware/MyBeanName.java @@ -2,9 +2,6 @@ package com.baeldung.aware; import org.springframework.beans.factory.BeanNameAware; -/** - * Created by Gebruiker on 4/25/2018. - */ public class MyBeanName implements BeanNameAware { @Override From 2146aa2be17aed5075bc0a8d5507f5049ea69c65 Mon Sep 17 00:00:00 2001 From: mherbaghinyan Date: Sun, 29 Apr 2018 11:26:56 +0400 Subject: [PATCH 4/4] BeanFactoryAware example tested --- .../src/main/java/com/baeldung/aware/AwareExample.java | 3 +++ spring-core/src/main/java/com/baeldung/aware/Config.java | 5 +++++ .../src/main/java/com/baeldung/aware/MyBeanFactory.java | 6 ++++-- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/spring-core/src/main/java/com/baeldung/aware/AwareExample.java b/spring-core/src/main/java/com/baeldung/aware/AwareExample.java index 20770c7f08..575d0d1792 100644 --- a/spring-core/src/main/java/com/baeldung/aware/AwareExample.java +++ b/spring-core/src/main/java/com/baeldung/aware/AwareExample.java @@ -13,5 +13,8 @@ public class AwareExample { = new AnnotationConfigApplicationContext(Config.class); MyBeanName myBeanName = context.getBean(MyBeanName.class); + + MyBeanFactory myBeanFactory = context.getBean(MyBeanFactory.class); + myBeanFactory.getMyBeanName(); } } diff --git a/spring-core/src/main/java/com/baeldung/aware/Config.java b/spring-core/src/main/java/com/baeldung/aware/Config.java index 547d02a60b..c75adc9335 100644 --- a/spring-core/src/main/java/com/baeldung/aware/Config.java +++ b/spring-core/src/main/java/com/baeldung/aware/Config.java @@ -10,4 +10,9 @@ public class Config { public MyBeanName getMyBeanName() { return new MyBeanName(); } + + @Bean + public MyBeanFactory getMyBeanFactory() { + return new MyBeanFactory(); + } } diff --git a/spring-core/src/main/java/com/baeldung/aware/MyBeanFactory.java b/spring-core/src/main/java/com/baeldung/aware/MyBeanFactory.java index 8bc5de9e45..6cdbd3dd7d 100644 --- a/spring-core/src/main/java/com/baeldung/aware/MyBeanFactory.java +++ b/spring-core/src/main/java/com/baeldung/aware/MyBeanFactory.java @@ -13,10 +13,12 @@ public class MyBeanFactory implements BeanFactoryAware { @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { - this.beanFactory = beanFactory; - System.out.println(beanFactory); } + public void getMyBeanName() { + MyBeanName myBeanName = beanFactory.getBean(MyBeanName.class); + System.out.println(beanFactory.isSingleton("myCustomBeanName")); + } }