Aware接口是一個標誌性接口,繼承此接口的接口xxxAware的實現類,在容器建立完成後,會回調實現方法,下面舉例:spring
1. 有不少xxxAware接口,下面舉兩個例子app
/** * description: 將實現xxxAware接口的Bean註冊到IOC容器中的時候,會將xxxAware的實現方法進行回調操做 * * @author 70KG * @date 2018/12/17 */ @Component public class MyAware implements ApplicationContextAware, BeanNameAware { private ApplicationContext applicationContext; private String beanName; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; Cat cat = (Cat) applicationContext.getBean("cat"); System.out.println("------->" + cat); } @Override public void setBeanName(String name) { this.beanName = name; System.out.println("------->" + beanName); } }
2. 配置類ide
/** * description * * @author 70KG * @date 2018/12/17 */ @Configuration public class MyConfig { @Bean public Cat cat() { return new Cat(); } @Bean public Dog dog() { return new Dog(); } @Bean public MyAware myAware() { return new MyAware(); } }
3. 測試類測試
/** * description * * @author 70KG * @date 2018/12/17 */ public class Test01 { public static void main(String[] args) { AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(MyConfig.class); Cat cat = (Cat) app.getBean("cat"); System.out.println("------->" + cat); } }
4. 結果this
------->myAware ------->com.nmys.story.springCore.springioc.importBean.Cat@78b66d36 ------->com.nmys.story.springCore.springioc.importBean.Cat@78b66d36