commit
0fda1fc877
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -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"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue