Interface:
public interface Manager {
Object read(Long id);
}
A class that implements this interface:
@Transactional
Public class ManagerImpl implements Manager {
@Override
public Object read(Long id) {
}
}
Aspect for ManagerImpl:
@Aspect
public class Interceptor {
@Pointcut("execution(public * manager.impl.*.*(..))")
public void executionAsManager() {
}
@Around("executionAsManager()")
public Object invoke(ProceedingJoinPoint joinPoint) throws Throwable {
return joinPoint.proceed();
}
}
Controller:
@RestController()
public class Controller {
@Autowired
private Manager manager;
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public Object read(@PathVariable Long id) {
return manager.read(id);
}
@RequestMapping(value = "reflection/{id}", method = RequestMethod.GET)
public Object readViaReflection(@PathVariable Long id) {
return ManagerImpl.class.getMethod("read", Long.class).invoke(manager, id);
}
}
So, when spring enters a control variable in the created controller proxy.
When calling the method directly:
manager.read(1L)
called out.
However, when I try to do this (see readViaReflection )
ManagerImpl.class.getMethod("read", Long.class).invoke(manager, 1L);
received object java.lang.reflect.InvocationTargetException is not an instance of a class declaration.
It is reasonable.
The question is: how can I call a method through reflection on a proxy created by spring (I have a method extracted from the target and I have a proxy instance created by spring).
, .