Browse Source

优化事件结构和说明

zhouhao 6 years ago
parent
commit
19c641feea

+ 2 - 0
hsweb-commons/hsweb-commons-entity/src/main/java/org/hswebframework/web/commons/entity/events/EntityModifyEvent.java

@@ -9,6 +9,8 @@ import java.io.Serializable;
 @Getter
 public class EntityModifyEvent<E> implements Serializable{
 
+    private static final long serialVersionUID = -7158901204884303777L;
+
     private E before;
 
     private E after;

+ 2 - 23
hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/main/java/org/hswebframework/web/service/GenericEntityService.java

@@ -18,7 +18,6 @@
 
 package org.hswebframework.web.service;
 
-import lombok.Setter;
 import org.hswebframework.web.commons.entity.GenericEntity;
 import org.hswebframework.web.commons.entity.LogicalDeleteEntity;
 import org.hswebframework.web.commons.entity.RecordCreationEntity;
@@ -30,8 +29,6 @@ import org.hswebframework.web.validator.group.CreateGroup;
 import org.hswebframework.web.validator.group.UpdateGroup;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.ApplicationEventPublisher;
-import org.springframework.context.PayloadApplicationEvent;
-import org.springframework.core.ResolvableType;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.Assert;
 import org.springframework.util.CollectionUtils;
@@ -118,17 +115,7 @@ public abstract class GenericEntityService<E extends GenericEntity<PK>, PK>
         //实现了 RecordModifierEntity接口的实体,将推送 EntityModifyEvent 事件.
         if (eventPublisher != null && pushModifyEvent()) {
             E old = selectByPk(pk);
-
-            EntityModifyEvent<?> e = new EntityModifyEvent<>(old, entity);
-            PayloadApplicationEvent<EntityModifyEvent<?>> event = new PayloadApplicationEvent<EntityModifyEvent<?>>(this, e) {
-                @Override
-                public ResolvableType getResolvableType() {
-                    return ResolvableType.forClassWithGenerics(PayloadApplicationEvent.class
-                            , ResolvableType.forClassWithGenerics(EntityModifyEvent.class, entityType));
-                }
-            };
-
-            eventPublisher.publishEvent(event);
+            eventPublisher.publishEvent(new GenericsPayloadApplicationEvent<>(this, new EntityModifyEvent<>(old, entity), entityType));
         }
         return createUpdate(entity)
                 //如果是RecordCreationEntity则不修改creator_id和creator_time
@@ -188,15 +175,7 @@ public abstract class GenericEntityService<E extends GenericEntity<PK>, PK>
         getDao().insert(entity);
 
         if (eventPublisher != null && pushCreatedEvent()) {
-            EntityCreatedEvent<?> e = new EntityCreatedEvent<>(entity);
-            PayloadApplicationEvent<EntityCreatedEvent<?>> event = new PayloadApplicationEvent<EntityCreatedEvent<?>>(this, e) {
-                @Override
-                public ResolvableType getResolvableType() {
-                    return ResolvableType.forClassWithGenerics(PayloadApplicationEvent.class
-                            , ResolvableType.forClassWithGenerics(EntityModifyEvent.class, entityType));
-                }
-            };
-            eventPublisher.publishEvent(event);
+            eventPublisher.publishEvent(new GenericsPayloadApplicationEvent<>(this, new EntityCreatedEvent<>(entity), entityType));
         }
         return entity.getId();
     }

+ 49 - 0
hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/main/java/org/hswebframework/web/service/GenericsPayloadApplicationEvent.java

@@ -0,0 +1,49 @@
+package org.hswebframework.web.service;
+
+import org.springframework.context.PayloadApplicationEvent;
+import org.springframework.core.ResolvableType;
+
+/**
+ * 动态泛型事件,用于动态发布支持泛型的事件
+ * <pre>
+ *     //相当于发布事件: EntityModifyEvent&lt;UserEntity&gt;
+ *     eventPublisher
+ *          .publishEvent(new GenericsPayloadApplicationEvent&lt;&gt;(this, new EntityModifyEvent<>(oldEntity, newEntity), UserEntity.class));
+ *
+ *      //只监听相同泛型事件
+ *      &#064;EventListener
+ *      public handleEvent(EntityModifyEvent&lt;UserEntity&gt; event){
+ *
+ *      }
+ * </pre>
+ *
+ * @author zhouhao
+ * @since 3.0.7
+ */
+public class GenericsPayloadApplicationEvent<E> extends PayloadApplicationEvent<E> {
+
+    private static final long serialVersionUID = 3745888943307798710L;
+
+    //泛型列表
+    private transient Class[] generics;
+
+    //事件类型
+    private transient Class eventType;
+
+    /**
+     * @param source   事件源
+     * @param payload  事件,不能使用匿名内部类
+     * @param generics 泛型列表
+     */
+    public GenericsPayloadApplicationEvent(Object source, E payload, Class... generics) {
+        super(source, payload);
+        this.generics = generics;
+        this.eventType = payload.getClass();
+    }
+
+    @Override
+    public ResolvableType getResolvableType() {
+        return ResolvableType.forClassWithGenerics(PayloadApplicationEvent.class
+                , ResolvableType.forClassWithGenerics(eventType, generics));
+    }
+}