• 观察者模式之Spring: event和listener


    pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>cn.it</groupId>
        <artifactId>springmvc-test</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-parent</artifactId>
            <version>2.2.2.RELEASE</version>
        </parent>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
    
        <build>
            <resources>
                <!-- 指定了java和resources下的xml文件,xlsx文件,xls文件打包到根目录 -->
                <resource>
                    <directory>src/main/java</directory>
                    <includes>
                        <include>*.xml</include>
                        <include>**/*.xlsx</include>
                        <include>**/*.xls</include>
                    </includes>
                </resource>
                <resource>
                    <directory>src/main/resources</directory>
                    <includes>
                        <include>**/*.yml</include>
                        <include>**/*.properties</include>
                        <include>**/*.xml</include>
                    </includes>
                    <filtering>false</filtering>
                </resource>
            </resources>
            <plugins>
                <!--解决打包没有mainClass问题-->
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>2.2.6.RELEASE</version>
                    <configuration>
                        <fork>true</fork>
                        <mainClass>com.power.app.AppWebApplication</mainClass>
                        <includeSystemScope>true</includeSystemScope>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
            <finalName>springmvc-test</finalName>
        </build>
    
    </project>
    View Code

    main

    package cn.it;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class ApplicationMain {
    
        public static void main(String[] args) {
            SpringApplication.run(ApplicationMain.class, args);
        }
    }
    View Code
    package cn.it.event;
    
    import org.springframework.context.ApplicationEvent;
    
    public class MyEvent extends ApplicationEvent {
    
        public MyEvent(Object source) {
            super(source);
            System.out.println("my Event");
        }
    
        public void print(){
            System.out.println("hello spring event[MyEvent]");
        }
    }
    package cn.it.event;
    
    import org.springframework.context.ApplicationEvent;
    import org.springframework.context.ApplicationListener;
    import org.springframework.stereotype.Component;
    
    @Component
    public class MyListener implements ApplicationListener {
    
        public void onApplicationEvent(ApplicationEvent event) {
            if(event instanceof MyEvent){
                System.out.println("into My Listener");
                MyEvent myEvent=(MyEvent)event;
                myEvent.print();
            }
        }
    }
    package cn.it.event;
    
    import org.springframework.beans.BeansException;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    import org.springframework.context.ApplicationEvent;
    import org.springframework.stereotype.Component;
    
    @Component
    public class MyPubisher implements ApplicationContextAware {
    
        private ApplicationContext applicationContext;
    
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            this.applicationContext=applicationContext;
        }
    
        public void publishEvent(ApplicationEvent event){
            System.out.println("into My Publisher's method");
            applicationContext.publishEvent(event);
        }
    }
    package cn.it.event;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import javax.annotation.Resource;
    
    @SpringBootTest
    @RunWith(SpringRunner.class)
    public class MyTest {
    
        @Resource
        MyPubisher myPublisher;
    
        @Test
        public void test(){
            MyEvent myEvent = new MyEvent("1");
            this.myPublisher.publishEvent(myEvent);
        }
    }
  • 相关阅读:
    服务器安装软件
    SQL server
    改变下blog思维
    react 父子组件互相通信
    linux下,文件的权限和数字对应关系详解
    Linux 下非root用户使用docker
    Two 观察者 observer pattern
    one 策略模式 strategy
    ssm maven spring AOP读写分离
    Unknown column in 'where clause'
  • 原文地址:https://www.cnblogs.com/smileblogs/p/13131914.html
Copyright © 2020-2023  润新知