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

          主站蜘蛛池模板: 卓尼县| 九龙县| 比如县| 桐乡市| 扎兰屯市| 绍兴市| 合江县| 桓仁| 井研县| 通江县| 中卫市| 佛教| 格尔木市| 文化| 安平县| 澎湖县| 涿州市| 新和县| 泽库县| 阳春市| 禹州市| 定南县| 积石山| 银川市| 常宁市| 五大连池市| 芜湖县| 永济市| 林州市| 吴桥县| 苍山县| 湾仔区| 白城市| 洛宁县| 贺兰县| 台北市| 前郭尔| 彰化市| 望奎县| 金湖县| 肥西县|