zoukankan      html  css  js  c++  java
  • 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容器生成的那一个,会产生冗余,所以不使用这种方式。

      

  • 相关阅读:
    4、redis之使用commons-pool
    好用的eclipse properties插件
    1、redis之安装与配置
    谷歌浏览器禁止window.close的问题
    在浏览器中使用JS打开并展示PDF文件
    JAVA遍历Map的方法
    GEOS库学习之三:空间关系、DE-9IM和谓词
    GEOS库的学习之二:简单几何图形的创建
    GEOS库的学习之一:介绍和编译
    GEOS库在windows中的编译和测试(vs2012)
  • 原文地址:https://www.cnblogs.com/youtang/p/11520882.html
Copyright © 2011-2022 走看看