Bean配置:
<bean id="logHelper" class="com.suning.app.dpc.core.helper.LogHelper"/>
<aop:config>
<aop:aspect id="logAspect" ref="logHelper">
<aop:pointcut id="logPointcut" expression="execution(* com.suning.app.dpc.core.service.impl.*.*(..))
|| execution(* com.suning.app.dpc.core.biz.impl.RsfProcessBizImpl.*(..))"/>
<aop:before pointcut-ref="logPointcut" method="before"/>
<aop:after-returning pointcut-ref="logPointcut" method="after" returning="ret"/>
</aop:aspect>
</aop:config>
切面处理类:
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSON;
public class LogHelper {
private static final Logger logger = LoggerFactory.getLogger(LogHelper.class);
public void before(JoinPoint jp) {
try {
logger.info("Method invoke befer: " + getMethodInfo(jp));
} catch (Exception e) {
logger.warn("Aop logger error.", e);
}
}
public void after(JoinPoint jp, Object ret) {
try {
logger.info("Method invoke after: " + getMethodInfo(jp) +", return value: " + toJson(ret));
} catch (Exception e) {
logger.warn("Aop logger error.", e);
}
}
private String getMethodInfo(JoinPoint jp) {
Object target = jp.getTarget();
Signature signature = jp.getSignature();
Object[] args = jp.getArgs();
String methodInfo = target.getClass().toString()+"."+signature.getName()+"("+toJson(args)+")";
return methodInfo;
}
private String toJson(Object obj) {
return JSON.toJSONString(obj);
}
}