paulwong

          SRPING自帶的事件監聽機制

          定義一個事件,因SPRING中可以有不同的事件,需要定義一個類以作區分:
          import lombok.Getter;
          import org.springframework.context.ApplicationEvent;


          @Getter
          public class JavaStackEvent extends ApplicationEvent {

              /**
               * Create a new {
          @code ApplicationEvent}.
               *
               * 
          @param source the object on which the event initially occurred or with
               *               which the event is associated (never {
          @code null})
               
          */
              public JavaStackEvent(Object source) {
                  super(source);
              }


          }

          定義一個此事件觀察者,即感興趣者:
          import lombok.NonNull;
          import lombok.RequiredArgsConstructor;
          import org.springframework.context.ApplicationListener;
          import org.springframework.scheduling.annotation.Async;

          /**
           * 觀察者:讀者粉絲
           
          */
          @RequiredArgsConstructor
          public class ReaderListener implements ApplicationListener<JavaStackEvent> {

              @NonNull
              private String name;

              private String article;

              @Async
              @Override
              public void onApplicationEvent(JavaStackEvent event) {
                  // 更新文章
                  updateArticle(event);
              }

              private void updateArticle(JavaStackEvent event) {
                  this.article = (String) event.getSource();
                  System.out.printf("我是讀者:%s,文章已更新為:%s\n", this.name, this.article);
              }

          }

          注冊感興趣者(將自身注入SPRING容器則完成注冊),并制定發布機制(通過CONTEXT發布事件):
          import lombok.extern.slf4j.Slf4j;
          import org.springframework.boot.CommandLineRunner;
          import org.springframework.context.ApplicationContext;
          import org.springframework.context.annotation.Bean;
          import org.springframework.context.annotation.Configuration;

          @Slf4j
          @Configuration
          public class ObserverConfiguration {

              @Bean
              public CommandLineRunner commandLineRunner(ApplicationContext context) {
                  return (args) -> {
                      log.info("發布事件:什么是觀察者模式?");
                      context.publishEvent(new JavaStackEvent("什么是觀察者模式?"));
                  };
              }

              @Bean
              public ReaderListener readerListener1(){
                  return new ReaderListener("小明");
              }

              @Bean
              public ReaderListener readerListener2(){
                  return new ReaderListener("小張");
              }

              @Bean
              public ReaderListener readerListener3(){
                  return new ReaderListener("小愛");
              }

          }

          posted on 2021-04-09 14:55 paulwong 閱讀(324) 評論(0)  編輯  收藏 所屬分類: SPRINGSPRING BOOT

          主站蜘蛛池模板: 馆陶县| 武强县| 尼玛县| 唐海县| 扬中市| 延安市| 瑞丽市| 邵阳市| 荣成市| 曲周县| 荥经县| 武强县| 阳谷县| 麻栗坡县| 西乌珠穆沁旗| 青海省| 赣州市| 临武县| 奎屯市| 海伦市| 利津县| 尼木县| 山阳县| 清原| 治多县| 托克逊县| 伊吾县| 邵东县| 哈尔滨市| 翁牛特旗| 报价| 博乐市| 确山县| 开化县| 外汇| 开江县| 锦州市| 邻水| 平陆县| 扶绥县| 石柱|