1.spring.xml 配置文件
<bean id="logAdvice" class="com.util.LogAdvice"></bean>
<!-- 使用CGLib库动态代理技术织入 --><aop:aspectj-autoproxy proxy-target-class="true"/> //使用自动代理方式,service实现类需要无参构造方法
<aop:config>
<aop:pointcut id="servicePointCut" expression="execution(* com.web.*.service..*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="servicePointCut" />
<!-- 日志记录拦截器 -->
<aop:aspect ref="logAdvice">
<aop:pointcut id="logPointCut" expression="execution(* com.web.system.action..*(..))"/> //拦截action操作的所有方法
<aop:before pointcut-ref="logPointCut" method="logBefore"/>
<aop:after-throwing pointcut-ref="logPointCut" method="logAfterThrow"/>
</aop:aspect>
</aop:config>
2.拦截器类
public class LogAdvice
{
private static Log log = LogFactory.getLog(LogAdvice.class);
public void logBefore(final JoinPoint joinPoint)
{
logMsg(joinPoint,"info");
}
/**
* 日志入库操作
* <功能详细描述>
* @param joinPoint
* @param operMsg
* @see [类、类#方法、类#成员]
*/
private void logMsg(final JoinPoint joinPoint,String operMsg)
{
String params = "";
String className = joinPoint.getTarget().getClass().getName(); //类型
String methodName = joinPoint.getSignature().getName(); //方法名
BaseAction target = (BaseAction)joinPoint.getTarget();
HttpServletRequest request = target.getRequest();
Enumeration enu = request.getParameterNames(); //获取参数名
//获取参数
while(enu.hasMoreElements()){
String paramName = (String)enu.nextElement(); //参数名
params +=paramName+":"+request.getParameter(paramName)+",";
}
String request_ip = request.getLocalAddr(); //获取ip
}
/**
* 捕获异常信息>
* <功能详细描述>
* @param joinPoint
* @see [类、类#方法、类#成员]
*/
public void logAfterThrow(final JoinPoint joinPoint) {
logMsg(joinPoint,"error");
}
}