Merge pull request #4139 from MherBaghinyan/BAEL-1717

Bael 1717
This commit is contained in:
Loredana Crusoveanu 2018-05-02 09:47:00 +03:00 committed by GitHub
commit 0fda1fc877
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 73 additions and 0 deletions

View File

@ -0,0 +1,20 @@
package com.baeldung.aware;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* Created by Gebruiker on 4/24/2018.
*/
public class AwareExample {
public static void main(String[] args) {
AnnotationConfigApplicationContext context
= new AnnotationConfigApplicationContext(Config.class);
MyBeanName myBeanName = context.getBean(MyBeanName.class);
MyBeanFactory myBeanFactory = context.getBean(MyBeanFactory.class);
myBeanFactory.getMyBeanName();
}
}

View File

@ -0,0 +1,18 @@
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();
}
@Bean
public MyBeanFactory getMyBeanFactory() {
return new MyBeanFactory();
}
}

View File

@ -0,0 +1,24 @@
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;
}
public void getMyBeanName() {
MyBeanName myBeanName = beanFactory.getBean(MyBeanName.class);
System.out.println(beanFactory.isSingleton("myCustomBeanName"));
}
}

View File

@ -0,0 +1,11 @@
package com.baeldung.aware;
import org.springframework.beans.factory.BeanNameAware;
public class MyBeanName implements BeanNameAware {
@Override
public void setBeanName(String beanName) {
System.out.println(beanName);
}
}