Spring Data - How interface class is working
Spring data is using a proxy pattern to implement the interface only class at runtime. The following example will show how it will override findById
function at runtime.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class ExampleDynamicProxy {
public static void main(String[] args) {
RepositoryFactorySupport factory = new RepositoryFactorySupport() {
@Override
public <T> T getRepository(Class<T> repositoryInterface) {
InvocationHandler handler = new JpaRepositoryInvocationHandler(repositoryInterface);
return (T) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{repositoryInterface}, handler);
}
};
ExampleRepository exampleRepository = factory.getRepository(ExampleRepository.class);
ExampleEntity entity = new ExampleEntity();
entity.setId(1L);
entity.setName("example");
exampleRepository.save(entity);
ExampleEntity retrievedEntity = exampleRepository.findById(1L).orElseThrow();
System.out.println(retrievedEntity.getName());
}
private interface ExampleRepository extends JpaRepository<ExampleEntity, Long> {
}
private static class ExampleEntity {
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
private static class JpaRepositoryInvocationHandler implements InvocationHandler {
private final Class<?> repositoryInterface;
JpaRepositoryInvocationHandler(Class<?> repositoryInterface) {
this.repositoryInterface = repositoryInterface;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) {
if (method.getName().startsWith("find")) {
System.out.println("Executing query: " + method.getName());
// return some dummy data
return new ExampleEntity();
} else {
throw new UnsupportedOperationException("Method " + method.getName() + " not implemented");
}
}
}
}