Aop拦截到的Action 使得@Autowired 无法自动装配问题解决 (二)

2014-11-24 11:54:49 · 作者: · 浏览: 78
nvocation methodInvocation) throws Throwable {

Object[] args = methodInvocation.getArguments();
String method = methodInvocation.getMethod().getName();
String action = methodInvocation.getMethod().getDeclaringClass().getName();

/**
* 由于Spring使用了Cglib代理,导致不能直接取得目标类名,需作此转换
*/
if (methodInvocation instanceof ReflectiveMethodInvocation) {
Object proxy = ((ReflectiveMethodInvocation) methodInvocation).getProxy();
action = StringUtils.substringBefore(proxy.toString(), "@");
/**
* 如使用了DispatchAction,将不能直接取得目标方法,需作此处理
*/
if (proxy instanceof DispatchAction) {
for (Object arg : args) {
if (arg instanceof HttpServletRequest)
method = ((HttpServletRequest) arg).getParameter("method");
}
}
}

/**
* 方法参数类型,转换成简单类型
*/
Class[] params = methodInvocation.getMethod().getParameterTypes();
String[] simpleParams = new String[params.length];
for (int i = 0; i < params.length; i++) {
simpleParams[i] = params[i].getSimpleName();
}

String simpleMethod = method + "(" + StringUtils.join(simpleParams, ",") + ")";

logger.info("{} 开始执行[{}]方法", action, method);

StopWatch clock = new StopWatch();
clock.start();
Object result = methodInvocation.proceed();
clock.stop();

logger.info("执行[{}]方法共消耗{}毫秒", simpleMethod, clock.getTime());

return result;
}
}
在applicationcontext.xml加入以下配置:
[xml]







就可以正确地以AOP的方式完成原本比较繁琐的功能了。
最近把框架升级到SS2H,顺便把Spring AOP实现由原来的Schema方式改为AspectJ方式,代码如下:
[java]
import org.apache.commons.lang.time.StopWatch;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
* 统计方法执行时间的工具类,采用Spring AOP方式实现.
*
* @author Kanine
*/
@Aspect
@Component
public class RunTimeHandler {

private static Logger logger = LoggerFactory.getLogger("code.coolbaby");

@Pointcut("execution(public String *()) && !execution(public String toString())" + " && target(code.coolbaby.core.CRUDActionSupport)")
void timer() {
}

@Around("timer()")
public Object time(ProceedingJoinPoint joinPoint) throws Throwable {

String clazz = joinPoint.getTarget().getClass().getSimpleName();
String method = joinPoint.getSignature().getName();

StopWatch clock = new StopWatch();
clock.start();
Object result = joinPoint.proceed();
clock.stop();

String[] params = new String[] { clazz, method, clock.getTime() + "" };
logger.info("[{}]执行[{}]方法共消耗[{}]毫秒", params);

return result;
}

}

import org.apache.commons.lang.time.StopWatch;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
* 统计方法执行时间的工具类,采用Spring AOP方式实现.
*
* @author Kanine
*/
@Aspect
@Component
public class RunTimeHandler {

private static Logger logger = LoggerFactory.getLogger("code.coolbaby");

@Pointcut("execution(public S