SpringAOP切面-aspectj记录操作日志

代码如下:

 

切面配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
		    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           	http://www.springframework.org/schema/context           
           	http://www.springframework.org/schema/context/spring-context-2.5.xsd
           	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
     
     
    <bean id="operService" class="com.study.aspectj.service.impl.operServiceImpl"/>
           	
    <!--切面类-->
    <bean id="logAspect" class="com.study.aspectj.aspectj.LogAspect"/>
    
	<!--
	         配置AOP, proxy-target-class用于设置是否强制使用cglib进行动态代理true表示强制使用,false则表示spring会根据目标对象有无实现接口来决定
                     使用jdk动态代理还是cglib代理。默认值就是false
    -->
 <aop:config>
     <aop:pointcut id="operlog" expression="@annotation(com.study.aspectj.annotation.BusyAnnotation)|| execution(* com.study..service.impl.*.*(..))"/>
     <!-- 引用上面装配的切面类的id -->
     <aop:aspect ref="logAspect">
         <!-- 环绕通知-->
         <aop:around method="around" pointcut-ref="operlog"/>
     </aop:aspect>
 </aop:config>
    
</beans>

 

切面类:

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<? extends Object> 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();
    	}
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值