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
    
  • 相关阅读:
    有固态硬盘的电脑还是不流畅?这些值得了解
    一名神舟笔记本电脑用户的内心独白
    验证码无法显示:Could not initialize class sun.awt.X11GraphicsEnvironment 解决方案
    nginx 负载均衡时,一台tomcat宕机时的问题 自动切换
    tomcat结合nginx使用小结
    金九银十面试突击,卧底去阿里、京东、美团、腾讯带回来的面试题(内含答案)
    干货分享 ▏Jmeter-场景设置/运行/参数化访问地址/【一次性打包呈现】
    各类APP功能测试用例/设计方法/数据库和日记分析——【范本】
    软件测试这些坑,千万不要踩!
    技术面试中,遇到不会回答的问题怎么破?来,教3招!
  • 原文地址:https://www.cnblogs.com/fly-book/p/12691671.html
Copyright © 2011-2022 走看看