package com.qc.aop.jdk.dao.impl;
import org.springframework.stereotype.Repository;
import com.qc.aop.jdk.dao.UserDao;
//目标类
@Repository("userDaoimpl")
public class UserDaoImpl implements UserDao{
@Override
public void addUser() {
System.out.println("添加用户");
}
@Override
public void deleteUser() {
System.out.println("删除用户");
}
}
package com.qc.aspectj.annocation;
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;
/**
* 切面类,在此类里面写通知
* @author asus_nootbook
*
*/
@Aspect //切面
@Component //生成切面类
public class MyAspect {
//定义切入点表达式
@Pointcut("execution(* com.qc.aop.jdk.*.*(..))")
//使用一个返回值为void,方法体为空的方法命名切入点
private void myPointCut() {}
//前置通知
@Before("myPointCut()")
public void myBefore(JoinPoint joinpoint) {
System.out.println("前置通知:模拟执行权限检查");
System.out.println("目标类是"+ joinpoint.getTarget());
System.out.println("被植入增强处理的目标方法为"+joinpoint.getSignature().getName());
}
//后置通知
@AfterReturning("myPointCut()")
public void myAfterReturning(JoinPoint joinpoint) {
System.out.println("后置通知:模拟记录日志");
System.out.println("被植入增强处理的目标方法为"+joinpoint.getSignature().getName());
}
/*
* 环绕通知
* proceedingjoinpoint是joinpoint子接口,表现可以实现目标方法
* 1.必须是Object类型的返回值
* 2/必须接收一个参数,类型为proceedingjoinpoint
* 3.必须throws Throwable
*/
@Around("myPointCut()")
public Object myAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
//开始
System.out.println("环绕开始,执行目标方法前,模拟开启事务");
//执行当前目标方法
Object obj=proceedingJoinPoint.proceed();
System.out.println("环绕结束,执行目标方法后,模拟关闭事物");
return obj;
}
//异常通知
@AfterThrowing(value="myPointCut()",throwing="e")
public void myAfterThrowing(JoinPoint joinPoint,Throwable e) {
System.out.println("异常通知:出错了"+e.getMessage());
}
//最终通知
@After("myPointCut()")
public void myAfter() {
System.out.println("模拟方法结束后释放资源");
}
}
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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.qc" ></context:component-scan>
<aop:aspectj-autoproxy />
</beans>
package com.qc.aspectj.annocation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.qc.aop.jdk.dao.UserDao;
public class TestAnnotation {
public static void main(String[] args) {
ApplicationContext applicationContext =new ClassPathXmlApplicationContext("com/qc/aspectj/annocation/applicationContent.xml");
UserDao userDao=(UserDao) applicationContext.getBean("userDaoimpl");
userDao.addUser();
}
}
运行结果
经过检查,最终发现execution()的包名存在问题
只需要把execution(* com.qc.aop.jdk.*.*(..))改为
execution(* com.qc.aop.jdk.dao.*.*(..))
就可以执行了