监听器

监听器模式复习

/**
 * 事件
 */
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
posted @ 2020-04-13 15:33  fly_bk  阅读(196)  评论(0编辑  收藏  举报