zoukankan      html  css  js  c++  java
  • 十三、【接口】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
    
  • 相关阅读:
    gitee之部署vue项目dist目录
    arcgis js 之 featureLayer创建模板
    arcgis js 之featureLayer服务查询及筛选
    video.js视频播放
    vue之监听对象、对象数组的改变
    Element-ui组件库Table表格导出Excel文件
    sublime 常用插件
    前端工具
    css 待处理
    移动前端开发:待处理
  • 原文地址:https://www.cnblogs.com/zhangjianbing/p/13726710.html
Copyright © 2011-2022 走看看