• 十三、【接口】Spring接口xxxAware


    Aware接口是一个标志性接口,继承此接口的接口xxxAware的实现类,在容器创建完成后,会回调实现方法,下面举例:

    1. 有很多xxxAware接口,下面举两个例子
    /**
     * description: 将实现xxxAware接口的Bean注册到IOC容器中的时候,会将xxxAware的实现方法进行回调操作
     * @author zhangjianbing
     * @date 2020年9月23日
     */
    @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);
        }
    }
    
    1. 配置类
    /**
     * @author zhangjianbing
     * @date 2020年9月23日
     */
    @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();
        }
    
    }
    
    1. 测试类
    /**
     * @author zhangjianbing
     * @date 2020年9月23日
     */
    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);
        }
    }
    
    1. 结果
    ------->myAware
    ------->com.nmys.story.springCore.springioc.importBean.Cat@78b66d36
    ------->com.nmys.story.springCore.springioc.importBean.Cat@78b66d36
    
  • 相关阅读:
    Oracle查询中将结果列按分组用逗号隔开,拼接成一行(listagg函数)
    Oracle SQL获取每个分组中日期最大的一条数据
    oracle字段根据字符串拆分,用subStr()和inStr()进行拆分
    javascript求最大公约数
    javascript单链表
    防input required属性的提示框
    css3绘制三色字
    js和php时间戳的问题
    ajax使用的一些错误
    浏览器js文件未及时更新问题
  • 原文地址:https://www.cnblogs.com/zhangjianbing/p/13726710.html
Copyright © 2020-2023  润新知