java通过aop保存日志信息到数据库

package com.ck.aopTest.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ck.aopTest.dao.MyAopDao;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.Map;

//执行顺序 环绕前,之前,环绕后,之后,之后@AfterReturning或@AfterThrowing

@Aspect
@Component
public class LogAspect {

private String type;
private String method;

//定义拦截规则
@Pointcut("execution(* get*(..)) || execution(* add*(..))")
private void aop() {
	
}
@Before("execution(* get*(..))")
public void insert(){
	type="1";
}
@Before("execution(* delete*(..))")
public void delete(){
	
}
@Before("execution(* update*(..))")
public void update(){
	
}
@Around("aop()")
public Object around(ProceedingJoinPoint joinPoint)  {
	System.out.println("调用环绕前");
	new Date();//开始时间
    Object proceed = null;
	try {
		joinPoint.proceed();
	} catch (Throwable e) {
		System.out.println("111111");
		e.printStackTrace();
	}

    //pdj.getArgs()获取参数值
    //pdj.getSignature().getName() 获取拦截方法
    //pdj.getTarget().getClass().getName();  获取类名

	System.out.println("调用环绕后");

	return proceed;
	
}

}

spring.xml

<?xml version="1.0" encoding="UTF-8"?>

<aop:aspectj-autoproxy />/开启aop注解
<context:component-scan base-package="com.qlzx.aopTest.*"></context:component-scan> 

pom.xml

4.0.0 com.qlzx SpringAop 0.0.1-SNAPSHOT
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.0.3.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>4.0.3.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>4.0.3.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.8.4</version>
</dependency>
<dependency>
    <groupId>javassist</groupId>
    <artifactId>javassist</artifactId>
    <version>3.12.1.GA</version>
</dependency>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值