使用Spring MVC
写了个拦截器用来拦截Controller中的方法:
@Override
public void before(Method method, Object[] args, Object target)
throws Throwable {
System.out.println("---------------");
System.out.println("method.getDeclaringClass().getName() = " + method.getDeclaringClass().getName());
System.out.println("method.getName()=" + method.getName());
System.out.println("----------------");
}
配置文件:
*Controller
btnInterceptor
但是运行观察控制台发现输出结果为:
[http-8080-1] DEBUG org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'app' processing
method.getDeclaringClass().getName() = java.lang.Object
[http-8080-1] DEBUG org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Mapping
[/safe/buildFuncTree.do] to handler 'cn.edu.swu.oa.safe.controller.SafeController@571688'
[http-8080-1] DEBUG org.springframework.web.bind.annotation.support.HandlerMethodInvoker - Invoking request handler
method: public void cn.edu.swu.oa.safe.controller.SafeController.buildFuncTree
method.getDeclaringClass().getName() = cn.edu.swu.oa.safe.controller.SafeController
method.getDeclaringClass().getName() = java.lang.Object
这部分是从哪来的,而且tomcat启动时也打印很多次这部分信息
那就得你自己写代码实现
在
public void before(Method method, Object[] args, Object target)
throws Throwable {
这里你可以判断method.getName()为Object的那些方法时直接return
System.out.println("---------------");
System.out.println("method.getDeclaringClass().getName() = " + method.getDeclaringClass().getName());
System.out.println("method.getName()=" + method.getName());
System.out.println("----------------");
}
toString 方法是那些Controller 类继承自 java.lang.Object 类的,
aop才不会管是不是继承父类的方法,只要是能访问到的方法都进行拦截嘛
你把target打印出来,更清楚点。
System.out.println("---------------");
System.out.println("method.getDeclaringClass().getName() = " + method.getDeclaringClass().getName());
System.out.println("method.getName()=" + method.getName());
System.out.println("target=" + target.getClass().getName());
System.out.println("----------------");
调用Controller实例的toString方法应该是在:AbstractUrlHandlerMapping.
// Eagerly resolve handler if referencing singleton via name.
if (!this.lazyInitHandlers && handler instanceof String) {
String handlerName = (String) handler;
if (getApplicationContext().isSingleton(handlerName)) {
handler = getApplicationContext().getBean(handlerName);
}
}
if (urlPath.equals("/")) {
if (logger.isDebugEnabled()) {
logger.debug("Root mapping to handler [" + handler + "]");
}
setRootHandler(handler);
}