zoukankan      html  css  js  c++  java
  • 监听器

    监听器模式复习

    /**
     * 事件
     */
    public abstract class WeatherEvent {
        public abstract String getWeather();
    }
    
    /**
     * 监听器
     */
    public interface WeatherListener {
        void onWeatherEvent(WeatherEvent event);
    }
    
    /**
     * 广播器
     */
    public interface EventMulticaster {
        void MulticastEvent(WeatherEvent event);
    
        void addListener(WeatherListener weatherListener);
    
        void removeLister(WeatherListener weatherListener);
    }
    
    
    @Component
    public abstract class AbstractEventMulticaster implements EventMulticaster{
        @Autowired
        private List<WeatherListener> list;
    
        @Override
        public void MulticastEvent(WeatherEvent event) {
            doStart();
            list.forEach(e->e.onWeatherEvent(event));
            doEnd();
        }
    
        protected abstract void doEnd();
    
        protected abstract void doStart();
    
        @Override
        public void addListener(WeatherListener weatherListener) {
            list.add(weatherListener);
        }
    
        @Override
        public void removeLister(WeatherListener weatherListener) {
            list.remove(weatherListener);
        }
    }
    
    public class SnowEvent extends WeatherEvent {
        @Override
        public String getWeather() {
            return "Snow";
        }
    }
    
    @Component
    public class SnowListener implements WeatherListener {
        @Override
        public void onWeatherEvent(WeatherEvent event) {
            if (event instanceof SnowEvent){
                System.out.println(event.getWeather());
            }
        }
    }
    
    @Component
    public class WeatherRunListener {
        @Autowired
        private WeatherEventMulticaster eventMulticaster;
    
        public void snow(){
            eventMulticaster.MulticastEvent(new SnowEvent());
        }
    
        public void rain(){
            eventMulticaster.MulticastEvent(new RainEvent());
        }
    }
    

    main测试

    public class Test {
        public static void main(String...args){
            WeatherEventMulticaster eventMulticaster = new WeatherEventMulticaster();
            RainListener rainListener = new RainListener();
            SnowListener snowListener = new SnowListener();
            eventMulticaster.addListener(rainListener);
            eventMulticaster.addListener(snowListener);
            eventMulticaster.MulticastEvent(new RainEvent());
            eventMulticaster.MulticastEvent(new SnowEvent());
            eventMulticaster.removeLister(rainListener);
            eventMulticaster.MulticastEvent(new RainEvent());
            eventMulticaster.MulticastEvent(new SnowEvent());
            //---start---
            //Rain
            //---end---
            //---start---
            //Snow
            //---end---
            //---start---
            //---end---
            //---start---
            //Snow
            //---end---
        }
    }
    

    springboot测试

    
    @SpringBootTest
    class SpringBootDemoApplicationTests {
    
        @Test
        void contextLoads() {
        }
    
        @Autowired
        private WeatherRunListener weatherRunListener;
    
        @Test
        void testEvent(){
            weatherRunListener.rain();
            weatherRunListener.snow();
        }
    }
    

    自定义ApplicationListener监听器两种方式

    import org.springframework.boot.context.event.ApplicationStartedEvent;
    import org.springframework.context.ApplicationListener;
    import org.springframework.core.annotation.Order;
    
    @Order
    public class FirstApplicationListener implements ApplicationListener<ApplicationStartedEvent> {
    
        @Override
        public void onApplicationEvent(ApplicationStartedEvent event) {
            System.out.println("FirstApplicationListener");
        }
    }
    
    
    import org.springframework.boot.context.event.ApplicationPreparedEvent;
    import org.springframework.boot.context.event.ApplicationStartedEvent;
    import org.springframework.context.ApplicationEvent;
    import org.springframework.context.event.SmartApplicationListener;
    import org.springframework.core.annotation.Order;
    
    @Order
    public class SecondApplicationListener implements SmartApplicationListener {
    
        @Override
        public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
            return ApplicationStartedEvent.class.isAssignableFrom(eventType)|| ApplicationPreparedEvent.class.isAssignableFrom(eventType);
        }
    
        @Override
        public void onApplicationEvent(ApplicationEvent event) {
            System.out.println("SecondApplicationListener");
        }
    }
    
    

    META-INF/spring.factories

    # Application Listeners
    org.springframework.context.ApplicationListener=
    cn.fly.springbootdemo.listener.FirstApplicationListener
    

    application.properties

    context.listener.classes=cn.fly.springbootdemo.listener.SecondApplicationListener
    
  • 相关阅读:
    关于requests.exceptions.SSLError: HTTPSConnectionPool(host='XXX', port=443)问题
    python Requests库总结
    fiddler实现手机抓包及手机安装证书报错“无法安装该证书 因为无法读取该证书文件”解决方法
    django接口的工作原理
    postman+newman+jenkins 持续集成搭建及使用,实现接口自动化
    Jmeter之JDBC Request及参数化
    selenium+Python中的面试总结
    UI自动化测试:页面截图的3种方法
    selenium中通过location和size定位元素坐标
    Allure+pytest生成测试报告
  • 原文地址:https://www.cnblogs.com/fly-book/p/12691671.html
Copyright © 2011-2022 走看看