1.aop配置
1.1导包
1.2 写目标类
package service; public class UserServiceImpl implements UserService { @Override public void save() { System.out.println("这是保存!"); //int i = 1/0; } @Override public void delete() { System.out.println("这是删除!"); } @Override public void update() { System.out.println("这是更改!"); } @Override public void find() { System.out.println("这是查询!"); } }
package service; public interface UserService { void save(); void delete(); void update(); void find(); }
1.3写通知类(其中spring中实现了五种通知类型,分别为:前置通知,后置通知,环绕通知,异常拦截通知,后置通知)
package cn.itcast.d_springaop; import org.aspectj.lang.ProceedingJoinPoint; //通知类 public class MyAdvice { //前置通知 // |-目标方法运行之前调用 //后置通知(如果出现异常不会调用) // |-在目标方法运行之后调用 //环绕通知 // |-在目标方法之前和之后都调用 //异常拦截通知 // |-如果出现异常,就会调用 //后置通知(无论是否出现 异常都会调用) // |-在目标方法运行之后调用 //---------------------------------------------------------------- //前置通知 public void before(){ System.out.println("这是前置通知!!"); } //后置通知 public void afterReturning(){ System.out.println("这是后置通知(如果出现异常不会调用)!!"); } //环绕通知 public Object around(ProceedingJoinPoint pjp) throws Throwable { System.out.println("这是环绕通知之前的部分!!"); Object proceed = pjp.proceed();//调用目标方法 System.out.println("这是环绕通知之后的部分!!"); return proceed; } //异常通知 public void afterException(){ System.out.println("出事啦!出现异常了!!"); } //后置通知 public void after(){ System.out.println("这是后置通知(出现异常也会调用)!!"); } }
1.4配置文件(applicationContext.xml)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" 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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd "> <!-- 准备工作: 导入aop(约束)命名空间 --> <!-- 1.配置目标对象 --> <bean name="userService" class="service.UserServiceImpl" ></bean> <!-- 2.配置通知对象 --> <bean name="myAdvice" class="cn.itcast.d_springaop.MyAdvice" ></bean> <!-- 3.配置将通知织入目标对象 --> <aop:config> <!-- 配置切入点 public void cn.itcast.service.UserServiceImpl.save() void cn.itcast.service.UserServiceImpl.save() * cn.itcast.service.UserServiceImpl.save() * cn.itcast.service.UserServiceImpl.*() * cn.itcast.service.*ServiceImpl.*(..) * cn.itcast.service..*ServiceImpl.*(..) service.UserServiceImpl.save() --> <aop:pointcut expression="execution(* service.*ServiceImpl.*(..))" id="pc"/> <aop:aspect ref="myAdvice" > <!-- 指定名为before方法作为前置通知 --> <aop:before method="before" pointcut-ref="pc" /> <!-- 后置 --> <aop:after-returning method="afterReturning" pointcut-ref="pc" /> <!-- 环绕通知 --> <aop:around method="around" pointcut-ref="pc" /> <!-- 异常拦截通知 --> <aop:after-throwing method="afterException" pointcut-ref="pc"/> <!-- 后置 --> <aop:after method="after" pointcut-ref="pc"/> </aop:aspect> </aop:config> </beans>
1.5测试
package cn.itcast.d_springaop; import javax.annotation.Resource; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import service.UserService; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:d_springaop/applicationContext.xml") public class Demo { @Resource(name="userService") private UserService us; @Test public void fun1(){ us.save(); } }结果为:
这是环绕通知之前的部分!! 这是前置通知!! 这是保存! 这是环绕通知之后的部分!! 这是后置通知(出现异常也会调用)!! 这是后置通知(如果出现异常不会调用)!!
2.aop注解配置
1.1目标类与上文一样
1.2通知类 改写
package cn.itcast.e_annotationaop; 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; //通知类 @Aspect//表示改类是一个通知类 public class MyAdvice { //这种是把切入点单独拿出来,后面简化使用 @Pointcut("execution(* service.*ServiceImpl.*(..))") public void pc(){} //前置通知 //指定该方法时前置通知,并制定切入点 @Before("MyAdvice.pc()") public void before(){ System.out.println("这是前置通知!!"); } //后置通知 @AfterReturning("execution(* service.*ServiceImpl.*(..))") public void afterReturning(){ System.out.println("这是后置通知(如果出现异常不会调用)!!"); } //环绕通知 @Around("execution(* service.*ServiceImpl.*(..))") public Object around(ProceedingJoinPoint pjp) throws Throwable { System.out.println("这是环绕通知之前的部分!!"); Object proceed = pjp.proceed();//调用目标方法 System.out.println("这是环绕通知之后的部分!!"); return proceed; } //异常通知 @AfterThrowing("execution(* service.*ServiceImpl.*(..))") public void afterException(){ System.out.println("出事啦!出现异常了!!"); } //后置通知 @After("execution(* service.*ServiceImpl.*(..))") public void after(){ System.out.println("这是后置通知(出现异常也会调用)!!"); } }
1.3 配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" 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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd "> <!-- 准备工作: 导入aop(约束)命名空间 --> <!-- 1.配置目标对象 --> <bean name="userService" class="service.UserServiceImpl" ></bean> <!-- 2.配置通知对象 --> <bean name="myAdvice" class="cn.itcast.e_annotationaop.MyAdvice" ></bean> <!-- 3.开启使用注解完成织入 --> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
1.4测试
package cn.itcast.e_annotationaop; import javax.annotation.Resource; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import service.UserService; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:e_annotationaop/applicationContext.xml") public class Demo { @Resource(name="userService") private UserService us; @Test public void fun1(){ us.save(); } }
结果为:
这是环绕通知之前的部分!! 这是前置通知!! 这是保存! 这是环绕通知之后的部分!! 这是后置通知(出现异常也会调用)!! 这是后置通知(如果出现异常不会调用)!!温馨提示:spring aop配置非常重要,希望这两种配置方法对您有所帮助!!!