zoukankan      html  css  js  c++  java
  • 自定义Spring event

    通过Spring自定义event

    首先我们定义我们的event类

    package com.hyenas.spring.custom.event;
    
    import org.springframework.context.ApplicationEvent;
    
    public class CustomEvent extends ApplicationEvent{
        
        private static final long serialVersionUID = -82737763905791865L;
    
        public CustomEvent(Object source) {
            super(source);
        }
    
        @Override
        public String toString() {
            return "my custom event";
        }
    
    
    }

    然后我们实现一个ApplicationListener ,这个ApplicationListener是带泛型的,带入我们自定义的event类

    package com.hyenas.spring.custom.event;
    
    import org.springframework.context.ApplicationListener;
    
    public class CustomEventHandler implements ApplicationListener<CustomEvent>{
    
        @Override
        public void onApplicationEvent(CustomEvent event) {
            System.out.println(event.toString());
        }
    
    }

    最后我们实现一个Publisher。

    package com.hyenas.spring.custom.event;
    
    import org.springframework.context.ApplicationEventPublisher;
    import org.springframework.context.ApplicationEventPublisherAware;
    
    public class CustomEventPublisher implements  ApplicationEventPublisherAware {
        
        private ApplicationEventPublisher publisher;
        
        @Override
        public void setApplicationEventPublisher(ApplicationEventPublisher publisher) {
            this.publisher = publisher;
        }
        
        
        public void publish() {
            CustomEvent event = new CustomEvent(this);
            publisher.publishEvent(event);
        }
    
    }

    在这里我们也可以实现一个ApplicationContextAware,因为ApplicationContext接口也是继承了我们的ApplicationEventPublisher接口的。Spring源码如下

    public interface ApplicationContext extends ListableBeanFactory, HierarchicalBeanFactory,
            MessageSource, ApplicationEventPublisher, ResourcePatternResolver

    我们实现ApplicationContextAware的话,会往我们的程序中setApplicationContext,ApplicationContext同样可以publishEvent

    然后,我们定义我们的spring xml文件custom-event.xml

    <?xml version="1.0" encoding="UTF-8"?>
    
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
       <bean id="customEventHandler" 
          class="com.hyenas.spring.custom.event.CustomEventHandler"/>
    
       <bean id="customEventPublisher" 
          class="com.hyenas.spring.custom.event.CustomEventPublisher"/>
    
    </beans>

    好,最后我们写一个MainApp测试一下我们的程序吧。

    package com.hyenas.spring.custom.event;
    
    import org.springframework.context.ConfigurableApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class MainApp {
         private static ConfigurableApplicationContext context;
    
        public static void main(String[] args) {
              context = new ClassPathXmlApplicationContext("custom-event.xml");
              
              CustomEventPublisher cvp = 
              (CustomEventPublisher) context.getBean("customEventPublisher");
              cvp.publish();  
              cvp.publish();
           }
    }

    运行结果

    my custom event
    my custom event

    这样我们就完成了整个event从建立,到触发的过程。

  • 相关阅读:
    NC13950 Alliances(LCA)
    NC13884 Paint Box(容斥原理)
    Gym102174B 炼金术(AC自动机)
    CF301D Yaroslav and Divisors(树状数组)
    CF1443D Extreme Subtraction(思维)
    Gym102174L 旅行的意义(期望dp)
    Gym102220F Mini-game Before Contest(博弈论)
    Gym102220D Master of Data Structure (虚树)
    用random.simple来解决从0-99这100个数中随机取10个不重复的数
    一个栈的输入序列为1,2,3,4,5,则下列序列中不可能是栈的输出序列的是()
  • 原文地址:https://www.cnblogs.com/hupengcool/p/3629318.html
Copyright © 2011-2022 走看看