SpringAOP切面-aspectj记录操作日志

代码如下:

SpringAOP切面-aspectj记录操作日志_第1张图片

 

切面配置文件:



     
     
    
           	
    
    
    
	
 
     
     
     
         
         
     
 
    

 

切面类:

package com.study.aspectj.aspectj;

import java.lang.reflect.Method;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;

import com.study.aspectj.annotation.BusyAnnotation;

public class LogAspect
{

	public void pointcut()
	{
	}

	public Object around(ProceedingJoinPoint pjp) throws Throwable
	{   
		System.out.println("====================================================>");
		System.out.println("进入切面类");
		Object result = null;
		try
		{   
			/*
			 切面能拿到的内容:
				public interface JoinPoint 
				{  
				   String toString();         //连接点所在位置的相关信息  
				   String toShortString();     //连接点所在位置的简短相关信息  
				   String toLongString();     //连接点所在位置的全部相关信息  
				   Object getThis();         //返回AOP代理对象  
				   Object getTarget();       //返回目标对象  
				   Object[] getArgs();       //返回被通知方法参数列表  
				   Signature getSignature();  //返回当前连接点签名  
				   SourceLocation getSourceLocation();//返回连接点方法所在类文件中的位置  
				   String getKind();        //连接点类型  
				   StaticPart getStaticPart(); //返回连接点静态部分  
				  } 
			*/
			Object[] args = pjp.getArgs();
			Class clazz = pjp.getTarget().getClass();
			String methodName = pjp.getSignature().getName();
			System.out.println("方法名"+methodName);
	        Class[] par=((MethodSignature) pjp.getSignature()).getParameterTypes();
	        Method method=clazz.getMethod(methodName, par);
			//获取方法上的注解
			BusyAnnotation annotation = method.getAnnotation(BusyAnnotation.class);
			System.out.println(annotation.module()+"~"+annotation.recordLog());
		}
		catch (Exception e)
		{

		}

		try
		{
			System.out.println("执行方法前");
			//调用方法
			result = pjp.proceed();
			//方法执行后
			System.out.println("执行方法后,方法返回值result:"+result);
		}
		catch (Throwable e)
		{

		}
		result ="切面类处理了方法返回值";
		System.out.println(result);
		System.out.println("<===================================================="+"\n\t");
		return result;
	}
}

注解:

package com.study.aspectj.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface BusyAnnotation
{
	public String module() default "";
    public boolean recordLog() default false;
}

需要使用切面的接口类和接口方法:

package com.study.aspectj.service;

public interface IoperService
{
   String openAccount(String account, String accountLevel);
   void addDescrible();
}


=========================================================================================



package com.study.aspectj.service.impl;

import java.util.concurrent.TimeUnit;


import com.study.aspectj.annotation.BusyAnnotation;
import com.study.aspectj.service.IoperService;


public class operServiceImpl implements IoperService
{

	/**
	 * 用户开户
	 */
	@BusyAnnotation(module = "开户", recordLog = true)
	@Override
	public String openAccount(String account, String accountLevel)
	{  
		// 模拟业务处理
		try
		{
			TimeUnit.SECONDS.sleep(3);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return String.format("手机号%s的用户开户成功了,开户级别%s", account,accountLevel);
	}

	@Override
	public void addDescrible()
	{
		System.out.println("添加描述");
	}

}

测试类:

package com.study.aspectj;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.study.aspectj.service.IoperService;

//使用junit4进行测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class AopTest extends AbstractJUnit4SpringContextTests
{   
	@Resource(name ="operService")
	private IoperService operService;
	
	@Test
    public void test() 
    {   
    	if(null != operService)
    	{
    		operService.openAccount("18625586623", "VIP");
    		operService.addDescrible();
    	}
    }
}

 

你可能感兴趣的:(spring,aspectj)