UserDao.java接口:
package com.liu.jdk;
public interface UserDao {
public void addUser();
public void deleteUser();
}
UserDaoImpl.java实现类/目标类:
package com.liu.jdk;
import org.springframework.stereotype.Repository;
//目标类
@Repository("userDao")
public class UserDaoImpl implements UserDao {
@Override
public void addUser() {
// TODO Auto-generated method stub
System.out.println("添加方法");
int i = 1/0;
}
@Override
public void deleteUser() {
// TODO Auto-generated method stub
System.out.println("删除方法");
}
}
MyAspect.java切面类:
package com.liu.aspect.annotation;
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.stereotype.Component;
//切面类
@Aspect
@Component
public class MyAspect {
@Pointcut(“execution(* com.liu.jdk..(…))”)
//需要一个返回值为void 方法体为空的方法来命名切入点
private void myPointCut() {}
//前置通知
@Before("myPointCut()")
public void before(JoinPoint joinPoint) {
System.out.println("前置通知开始,模拟检查权限 ");
System.out.println("目标类:"+joinPoint.getTarget());
System.out.println("目标方法为:"+joinPoint.getSignature().getName());
}
//后置通知
@AfterReturning("myPointCut()")
public void afterRunning(JoinPoint joinPoint) {
System.out.println("后置通知开始,模拟记录日志");
System.out.println("目标方法为:"+joinPoint.getSignature().getName());
}
//环绕通知
@Around("myPointCut()")
public Object around(ProceedingJoinPoint proceedingJoinPoint)throws Throwable {
System.out.println("环绕通知开始");
Object obj = proceedingJoinPoint.proceed();
System.out.println("环绕通知结束");
return obj;
}
//异常通知
@AfterThrowing(value = "myPointCut()",throwing = "e")
public void afterThrowing(JoinPoint joinPoint,Throwable e) {
System.out.println("异常通知,原因在于:"+e.getMessage());
}
//最终通知
@After("myPointCut()")
public void after() {
System.out.println("最终通知");
}
}
ApplicationContext.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd ">
<!-- 指定需要扫描的包 开启注解-->
<context:component-scan base-package="com.liu"></context:component-scan>
<!-- 启动基于注解的声明式AspectJ支持 -->
<aop:aspectj-autoproxy />
</beans>
Test测试类:
package com.liu.aspect.annotation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.liu.jdk.UserDao;
public class Test {
public static void main(String[] args) {
String xmlpath = "com/liu/aspect/annotation/ApplicationContext.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlpath);
UserDao userDao = (UserDao) applicationContext.getBean("userDao");
userDao.addUser();
}
}
运行结果:
环绕通知开始
前置通知开始,模拟检查权限
目标类:com.liu.jdk.UserDaoImpl@5158b42f
目标方法为:addUser
添加方法
最终通知
异常通知,原因在于:/ by zero