An interface:
public interface Manager {
Object read(Long id);
}
A class which implements this interface:
@Transactional
Public class ManagerImpl implements Manager {
@Override
public Object read(Long id) {
// Implementation here
}
}
An aspect for ManagerImpl:
@Aspect
public class Interceptor {
@Pointcut("execution(public * manager.impl.*.*(..))")
public void executionAsManager() {
}
@Around("executionAsManager()")
public Object invoke(ProceedingJoinPoint joinPoint) throws Throwable {
// Do some actions
return joinPoint.proceed();
}
}
A 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 injects manager variable within controller proxy created.
When method invoked directly:
manager.read(1L)
aspect is invoked.
However, when I try to do like this (see readViaReflection)
ManagerImpl.class.getMethod("read", Long.class).invoke(manager, 1L);
got java.lang.reflect.InvocationTargetException object is not an instance of declaring class.
Which is reasonable.
The question is: how can I invoke method via reflection on proxy-object created by spring (I have method extracted from target-object and I have instance of proxy created by spring).
Can not do invocation on target because then aspect will not invoke.
Aucun commentaire:
Enregistrer un commentaire