• ApplicationContextAware接口实现多继承Bean装配


    问题背景

      使用多渠道给用户发送短信,但入口只有一个,并且以后可能会摒弃或扩展渠道,所以使用继承来实现。

      首先父类接口Sender定义发送短信等一些基础公共方法,主要如下:

    public interface Sender {
    
        void send();
    }

      各渠道作为子类继承Sender接口,实现基础方法,如下:

    
    
    @Service
    public class BaseSender1 implements Sender {
    @Override
    public void send() {
    System.out.println("BaseSender1.send()");
    }
    }
    @Service
    public class BaseSender2 implements Sender {
    @Override
    public void send() {
    System.out.println("BaseSender2.send()");
    }
    }
    @Service
    public class BaseSender3 implements Sender {
    @Override
    public void send() {
    System.out.println("BaseSender3.send()");
    }
    }

      在发送短信时,需要得到渠道的集合,从中选择一个发送短信,此处就用到了ApplicationContextAware,先看一下具体实现:

    @Component
    @Slf4j
    public class SenderUtil implements ApplicationContextAware {
    
        private Collection<Sender> senders;
    
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            log.info("applicationContext:{}", applicationContext);
            senders = applicationContext.getBeansOfType(Sender.class).values();
            senders.forEach(Sender::send);
        }
    
        public Collection<Sender> getSenders(){
            return senders;
        }
    }

      SenderUtil继承ApplicationContextAware接口,实现setApplicationContext方法,在setApplicationContext方法中,我们得到applicationContext上下文情况,获取Type为Sender的Bean,并且将其实例化放入map中,最后遍历map的value得到Sender子类集合。

    实现过程

      实现ApplicationContextAware接口的类通过Spring容器,在启动时自动调用setApplicationContext方法,将applicationContext注入。实现了ApplicationContextAware接口的类需要在容器内进行注册,可使用@Component注解或在配置文件中配置。在使用ApplicationContextAware获得上下文时,必须确保当前运行的代码与Spring代码处于同一个上下文,否则获得的applicationContext为空。

      ps:为什么不使用ClassPathXmlApplicationContext加载配置文件获得上下文环境呢,这样做是可以的,但是此时获得的applicationContext并不是Spring容器生成的那一个,会产生冗余,所以不使用这种方式。

      

  • 相关阅读:
    什么是Netflix Feign?它的优点是什么?
    Spring Boot 自动配置原理是什么?
    springcloud断路器作用?
    什么是SpringCloudConfig?
    find命令查找包含指定内容的文件
    @PostConstruct使用总结
    @Retention 注解的作用
    SpringBoot自定义Condition注解
    Spring Boot 入门
    SpringBoot +MSSQL
  • 原文地址:https://www.cnblogs.com/youtang/p/11520882.html
Copyright © 2020-2023  润新知