Skip to content

Commit a647a9e

Browse files
committed
add domain event
1 parent d7fd2c6 commit a647a9e

File tree

7 files changed

+118
-2
lines changed

7 files changed

+118
-2
lines changed

springboot-starter/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@
6666
<artifactId>spring-boot-starter-web</artifactId>
6767
<scope>test</scope>
6868
</dependency>
69+
<dependency>
70+
<groupId>org.hibernate.orm</groupId>
71+
<artifactId>hibernate-core</artifactId>
72+
</dependency>
6973

7074
</dependencies>
7175

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.codingapi.springboot.framework.domain;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.cglib.proxy.Enhancer;
5+
import org.springframework.cglib.proxy.MethodInterceptor;
6+
import org.springframework.cglib.proxy.MethodProxy;
7+
8+
import java.lang.reflect.Method;
9+
10+
@Slf4j
11+
public class EntityEventInterceptor implements MethodInterceptor {
12+
13+
private final Object target;
14+
15+
public EntityEventInterceptor(Object target) {
16+
this.target = target;
17+
}
18+
19+
public Object createProxy() {
20+
Enhancer enhancer = new Enhancer();
21+
enhancer.setSuperclass(target.getClass());
22+
enhancer.setCallback(this);
23+
return enhancer.create();
24+
}
25+
26+
27+
@Override
28+
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
29+
String methodName = method.getName();
30+
log.info("EntityEventProxy invoke method:{}", methodName);
31+
log.info("EntityEventProxy invoke args:{}", args);
32+
log.info("EntityEventProxy invoke target:{}", target);
33+
return method.invoke(target, args);
34+
}
35+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.codingapi.springboot.framework.domain;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.cglib.proxy.Proxy;
5+
6+
7+
@Slf4j
8+
public class EntityEventProxy {
9+
10+
public static <T> T createProxy(T target) {
11+
return (T) Proxy.newProxyInstance(
12+
target.getClass().getClassLoader(),
13+
target.getClass().getInterfaces(),
14+
(proxy, method, args) -> {
15+
String methodName = method.getName();
16+
log.info("EntityEventProxy invoke method:{}", methodName);
17+
log.info("EntityEventProxy invoke args:{}", args);
18+
log.info("EntityEventProxy invoke target:{}", target);
19+
return method.invoke(target, args);
20+
});
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.codingapi.springboot.framework.domain;
2+
3+
import java.lang.reflect.Constructor;
4+
import java.lang.reflect.InvocationTargetException;
5+
6+
public class EntityProxyFactory {
7+
8+
public static <T> T createEntity(Class<T> entityClass, Object... args) {
9+
try {
10+
Class<?>[] parameterTypes = new Class<?>[args.length];
11+
for (int i = 0; i < args.length; i++) {
12+
parameterTypes[i] = args[i].getClass();
13+
}
14+
Constructor<T> constructor = entityClass.getConstructor(parameterTypes);
15+
T entity = constructor.newInstance(args);
16+
EntityEventInterceptor interceptor = new EntityEventInterceptor(entity);
17+
return (T)interceptor.createProxy();
18+
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
19+
throw new RuntimeException("Failed to create entity", e);
20+
}
21+
}
22+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.codingapi.springboot.framework.domain;
2+
3+
import com.codingapi.springboot.framework.event.IEvent;
4+
5+
public class EventProxyEvent implements IEvent {
6+
7+
private Object entity;
8+
9+
private long timestamp;
10+
11+
private String field;
12+
private Object oldField;
13+
private Object newField;
14+
}

springboot-starter/src/test/java/com/codingapi/springboot/framework/domain/Demo.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
import com.codingapi.springboot.framework.serializable.JsonSerializable;
66
import com.codingapi.springboot.framework.serializable.MapSerializable;
77
import lombok.Getter;
8+
import lombok.NoArgsConstructor;
9+
import lombok.ToString;
810

11+
@ToString
12+
@NoArgsConstructor
913
public class Demo implements JsonSerializable, MapSerializable {
1014

1115
@Getter
@@ -19,10 +23,10 @@ public Demo(String name) {
1923
}
2024

2125
public void changeName(String name) {
22-
String beforeName = this.name;
26+
// String beforeName = this.name;
2327
this.name = name;
2428
//push event
25-
EventPusher.push(new DemoChangeEvent(beforeName, name));
29+
// EventPusher.push(new DemoChangeEvent(beforeName, name));
2630
}
2731

2832
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.codingapi.springboot.framework.domain;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springframework.boot.test.context.SpringBootTest;
5+
6+
@SpringBootTest
7+
class EntityProxyFactoryTest {
8+
9+
@Test
10+
void createEntity() {
11+
Demo demo = EntityProxyFactory.createEntity(Demo.class, "test");
12+
System.out.println(demo);
13+
demo.changeName("123");
14+
}
15+
}

0 commit comments

Comments
 (0)