• 监听器


    监听器模式复习

    /**
     * 事件
     */
    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
    
  • 相关阅读:
    原始字符串
    .Net Core 常见错误解决记录
    P1010 幂次方 P1022 计算器的改良
    P1036 选数
    广度优先遍历
    P4327 彼得潘框架
    链表
    标准库与标准模板库
    信息学竞赛打表犯规吗?
    对拍程序
  • 原文地址:https://www.cnblogs.com/fly-book/p/12691671.html
Copyright © 2020-2023  润新知