Я создал динамический прокси:
Код: Выделить всё
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.isAnnotationPresent(Query.class)) {
System.out.println("It's a query!");
} else {
System.out.println("Not a query!");
}
Object result = method.invoke(repository, args);
System.out.println("After " + method.getName() + " is called");
return result;
}
Код: Выделить всё
if (CrudRepository.class.isAssignableFrom(node.type)) {
Class[] interfaces = node.type.getInterfaces().length > 0
? node.type.getInterfaces()
: new Class[]{ node.type };
InvocationHandler handler = new CrudInvocationHandler((CrudRepository) bean);
bean = Proxy.newProxyInstance(
node.type.getClassLoader(),
interfaces,
handler
);
}
Код: Выделить всё
@Bean
public UserRepository userRepository() {
return new UserRepository();
}
Как он может вернуть экземпляр прокси для интерфейса репозитория (например, UserRepository), даже если реального класса реализации нет?
Подробнее здесь: https://stackoverflow.com/questions/797 ... positories
Мобильная версия