spring aop 切面添加日志

这是一个非常简单的spring aop切面添加日志的程序,下面来看一下这个程序

1、程序使用jar包

spring aop 切面添加日志_第1张图片

2、切面类LoggingAspect.java

package com.cailei.aop.aspect;

import java.util.Arrays;

import org.aspectj.lang.JoinPoint;

public class LoggingAspect {

	public void beforeMethod(JoinPoint joinPoint) {

		String methodName = joinPoint.getSignature().getName();
		Object[] args = joinPoint.getArgs();
		long times = System.currentTimeMillis();
		System.out.println("start execute time:" + times + "," + methodName + " start execute,args:" + Arrays.toString(args));
	}

	public void afterMethod(JoinPoint joinPoint) {
		String methodName = joinPoint.getSignature().getName();
		long times = System.currentTimeMillis();
		System.out.println("after execute time:" + times + "," + methodName + " execute end");
	}

	public void afterReturning(JoinPoint joinPoint, Object result) {
		String methodName = joinPoint.getSignature().getName();
		System.out.println(methodName + " execute result:" + result);
	}
	
	public void afterThrowing(JoinPoint joinPoint, Exception e) {
		String methodName = joinPoint.getSignature().getName();
		System.out.println(methodName + " execute exception:" + e);
	}

}
 

3、业务类LoginService.java

package com.cailei.aop;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.cailei.aop.service.LoginService;

public class Test {

	public static void main(String[] args) throws Exception {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		LoginService ls = (LoginService)ac.getBean("loginService");
		ls.login("lucy", "123456");
		ls.login("lucy", "111111");
		
	}
}
 

4、spring配置文件applicationContext.xml



         
         
         
         
         
         	
         	
         		
         		
         		
         		
         	
         

运行结果

spring aop 切面添加日志_第2张图片

你可能感兴趣的:(spring工程)