使用注解定义切面(增强方法)
package aop;
import java.util.Arrays;
import org.apache.log4j.Logger;
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;
/**
* 使用注解定义切面
* @params
* @author 冯军红
* @return
*
*/
@Aspect
public class UserLogger {
private static final Logger log=Logger.getLogger(UserLogger.class);
//定义切入点(即:目标方法)
@Pointcut("execution(* service.impl.UserServiceImpl.*(..))")
public void pointcut(){}
//@Before("pointcut()")
public void before(JoinPoint jp) {
log.info("\n我是前置增强\n调用" + jp.getTarget() + "的" + jp.getSignature()
+ "方法。方法参数:" + Arrays.toString(jp.getArgs())+"\n");
}
//@AfterReturning(pointcut="pointcut()",returning="result")
public void afterReturning(JoinPoint jp,Object result) {
log.info("\n我是后置增强\n调用" + jp.getTarget() + "的" + jp.getSignature()
+ "方法。方法返回值::" +result+"\n");
}
//@AfterThrowing(pointcut="pointcut()",throwing="e")
public void afterThrowing(JoinPoint jp,RuntimeException e){
log.error(jp.getSignature().getName()+"方法发生异常:"+e);
}
//@Around("pointcut()")
public Object aroundLogger(ProceedingJoinPoint jp)throws Throwable {
log.info("调用"+jp.getTarget()+"的"+jp.getSignature().getName()+"方法。方法入参:"+Arrays.toString(jp.getArgs()));
try {
Object result=jp.proceed();
log.info("调用"+jp.getTarget()+"的"+jp.getSignature().getName()+"方法。方法返回值:"+result);
return result;
} catch (Exception e) {
log.error(jp.getSignature().getName()+"方法发生异常:"+e);
throw e;
} finally{
log.info(jp.getSignature().getName()+"方法结束执行");
}
}
//@After("pointcut()")
public void afterLogger(JoinPoint jp){
log.info(jp.getSignature().getName()+"方法结束执行。");
}
}
配置xml文件
注意:导入aop命名空间,添加 <aop:aspectj-autoproxy />元素
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 扫描包中注解标注的类 -->
<context:component-scan base-package="dao.impl,service.impl"/>
<!-- 把增强方法定义为bean,交给spring管理 -->
<bean class="aop.UserLogger"/>
<!-- 开启对@AspectJ注解的支持 -->
<aop:aspectj-autoproxy />
</beans>
测试以上代码
package com.bdqn;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.UserService;
import entity.TestEntity;
import entity.User;
public class test {
@Test
public void eg1() {
// 使用ApplicationContext接口的实现类ClassPathXmlApplicationContext加载Spring配置文件
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService service = (UserService) ctx.getBean("userService");
User user=new User();//此处传入一个空对象
service.addNewUser(user);
}
}
控制台输入信息:
我是前置增强
调用service.impl.UserServiceImpl@34c7e8a7的void service.UserService.addNewUser(User)方法。方法参数:[entity.User@34e93999]
保存用户信息到数据库
用户名:null 密码:null