AspectJ的六种通知的类型,最后一种不讲,只讲前五种.
环绕通知是可以阻止目标方法执行的.
<?xml version="1.0" encoding="UTF-8"?>
<!-- 引AOP的约束了,Schema里面必须是带有AOP的. -->
<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"
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">
<!-- 自动的生成代理 底层就是AnnotationAwareAspectJAutoProxyCreator-->
<aop:aspectj-autoproxy />
<bean id="userDao" class="cn.itcast.spring3.demo1.UserDao"></bean> <bean id="myAspect" class="cn.itcast.spring3.demo1.MyAspect"></bean> </beans>
package cn.itcast.spring3.demo1; public class UserDao {
public void add(){
System.out.println("添加用户");
}
//public void update(){
public int update(){
System.out.println("修改用户");
return 1;
}
public void delete(){
System.out.println("删除用户");
}
public void find(){
//int i = 1/0;
System.out.println("查询用户");
}
}
package cn.itcast.spring3.demo1; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringTest1 {
@Autowired
@Qualifier("userDao")
private UserDao userDao;
@Test
public void demo1(){
userDao.add();
userDao.delete();
userDao.update();
userDao.find();
}
}
package cn.itcast.spring3.demo1; 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; //我的切面类
/**
* 切面类:就是切点与增强的结合
* 切点就是哪些方法要使用什么增强
* @author zhongzh
*
*/
@Aspect //用来定义切面
public class MyAspect {
//写增强的代码,假设现在做的是一个前置增强.
@Before(value="execution(* cn.itcast.spring3.demo1.UserDao.add(..))") //前置增强的注解是Before 刚一写完Before就报错了,它里面肯定是有属性没写的.学过JDK的注解,如果只想使用它里面的value属性,value是可以不写的.
//现在只对UserDao.add()应用前置增强.
public void before(JoinPoint joinPoint){//JointPoint是可以用作增强的一个点.就是被增强的那个方法的描述.
//System.out.println("前置增强......");
System.out.println("前置增强......"+joinPoint);
}
@AfterReturning(value="execution(* cn.itcast.spring3.demo1.UserDao.update(..))",returning="returnVal")//这里也是要写切面表达式
//public void afterReturn(){
public void afterReturn(Object returnVal){
System.out.println("后置增强....方法的返回值:"+returnVal);
}
@Around(value="execution(* cn.itcast.spring3.demo1.UserDao.find(..))")
public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
System.out.println("环绕前增强.....");
Object obj = proceedingJoinPoint.proceed();
System.out.println("环绕后增强.....");
return obj;//如果想要阻止目标方法的执行这里可以return null
}
@AfterThrowing(value="execution(* cn.itcast.spring3.demo1.UserDao.find(..))",throwing="e")
public void afterThrowing(Throwable e){
System.out.println("不好了 出异常了!!!!"+e.getMessage());
}
@After(value="execution(* cn.itcast.spring3.demo1.UserDao.find(..))") //最终通知 类似于try catch finally里面的那个finally
public void after(){
System.out.println("最终通知....");//最终通知它不管你什么情况下都会执行
} }