AspectJ
通知类型:
- 前置通知 before
- 后置通知 after-returning
- 最终通知 after
- 环绕通知 around
- 抛出异常通知 after-throwing
写一个切面类
import org.aspectj.lang.ProceedingJoinPoint;
public class MyAspect03 {
public void before(){
//前置通知
System.out.println("这是前置通知。。。");
}
public void afterReturning(){
//后置通知
System.out.println("这是后置通知。。。");
}
public void afterThrowing(Throwable e){
//异常通知
System.out.println("这是异常通知。。。");
}
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("这是环绕通知。。。前");
System.out.println(pjp.getSignature().getName());
Object obj = pjp.proceed();
//环绕通知
System.out.println("这是环绕通知。。。后");
return obj;
}
}
//测试:
@Test
public void test14(){
ApplicationContext c = new ClassPathXmlApplicationContext("beans7.xml");
PersonService ps = (PersonService) c.getBean("personService");
ps.addPerson();
ps.delPerson();
}
/*输出
这是前置通知。。。
这是环绕通知。。。前
addPerson
增加用户。。。
这是环绕通知。。。后
这是后置通知。。。
这是前置通知。。。
这是环绕通知。。。前
delPerson
这是异常通知。。。
*/
配置xml
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--配置PersonService-->
<bean id="personService" class="com.hetl.day04.service.PersonServiceImpl"></bean>
<!--配置切面对象-->
<bean id = "myAspect" class="com.hetl.day05.aspect.MyAspect03"/>
<!--配置aop-->
<aop:config>
<aop:aspect ref="myAspect">
<!--定义一个切入点-->
<aop:pointcut id="myPointcut" expression="execution(* com.hetl.day04.service.*.*(..))"/>
<!--配置前置通知-->
<aop:before method="before" pointcut-ref="myPointcut"></aop:before>
<!--配置后置通知-->
<aop:after-returning method="afterReturning" pointcut-ref="myPointcut"></aop:after-returning>
<!--抛出异常通知-->
<aop:after-throwing method="afterThrowing" pointcut-ref="myPointcut" throwing="e"></aop:after-throwing>
<!--环绕通知-->
<aop:around method="around" pointcut-ref="myPointcut"></aop:around>
</aop:aspect>
</aop:config>
</beans>
AspectJ基于注解
package com.hetl.day05.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class MyAspect03 {
//声明一个公共切入点
@Pointcut("execution(* com.hetl.day04.service.*.*(..))")
public void myPointcut(){
}
@Before("execution(* com.hetl.day04.service.*.*(..))")
public void before(JoinPoint point){
//前置通知
System.out.println("这是前置通知。。。"+point.getSignature().getName());
}
@AfterReturning("execution(* com.hetl.day04.service.*Impl.*(..))")
public void afterReturning(){
//后置通知
System.out.println("这是后置通知。。。");
}
@AfterThrowing(pointcut = "myPointcut()",throwing = "e")
public void afterThrowing(Throwable e){
//异常通知
System.out.println("这是异常通知。。。"+e.getMessage());
}
@Around("myPointcut()")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("这是环绕通知。。。前");
System.out.println(pjp.getSignature().getName());
Object obj = pjp.proceed();
//环绕通知
System.out.println("这是环绕通知。。。后");
return obj;
}
}
@Test
public void test15(){
ApplicationContext c = new ClassPathXmlApplicationContext("beans8.xml");
PersonService ps = (PersonService) c.getBean("personService");
ps.addPerson();
}
xml配置
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.hetl.day04.service"/>
<context:component-scan base-package="com.hetl.day05.aspect"/>
<!--配置aop注解生效-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>