springAop注解式Demo
package AnnoAspect.Aspect; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component; /** * 切面类 */ @Component @Aspect public class MyAspect2 { /** * 前置通知 */ @Before(value = "execution(* AnnoAspect.dao.*.save(..))") public void before(){ System.out.println("前置通知"); } /** * 后置通知 并且拿到方法返回值 */ //@AfterReturning(value = "execution(* AnnoAspect.dao.*.delete(..))",returning = "result") @AfterReturning(value = "MyAspect2.ponitCut1()",returning = "result")//使用同一切入点注解 public void afterReturn(Object result){ System.out.println("后置通知"+result); } /** * 环绕通知 */ @Around(value = "execution(* AnnoAspect.dao.*.update(..))" ) public Object around(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("环绕通知前"); Object proceed = joinPoint.proceed(); System.out.println("环绕通知后"); return proceed; } @AfterThrowing(value = "execution(* AnnoAspect.dao.*.find(..))",throwing = "e") public void throwing(Throwable e){ System.out.println("异常抛出通知!!!"); System.out.println(e.getMessage());//获得异常信息 } @After(value = "execution(* AnnoAspect.dao.*.find(..))") public void After(){ System.out.println("最终通知!!!"); } //切入点注解 相当于有一个方法 有多种通知 相当于将切入点其封装下 @Pointcut(value = "execution(* AnnoAspect.dao.*.delete(..)))") private void ponitCut1(){ }//方法并无实际意义 }
package AnnoAspect.dao; import org.springframework.stereotype.Repository; //这边不适用接口 考察aop的cglib动态代理 @Repository("orderDao") public class OrderDao { public void save() { System.out.println("add order!"); } public String delete() { System.out.println("delete order!"); return "abc"; } public void update() { System.out.println("update order!"); } public void find() { System.out.println("find order!"); int i=1/0; } }
package AnnoAspect.test; import AnnoAspect.dao.OrderDao; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.annotation.Resource; @RunWith(value = SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml" ) public class Test { @Resource(name = "orderDao") private OrderDao orderDao; @org.junit.Test public void test1() { orderDao.delete(); } }
注意:这边目标类并没有实现接口,所以切面类进行增强的时候是通过cglib动态代理的方式进行方法的扩展增强!
<?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" xmlns:tx="http://www.springframework.org/schema/tx" 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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--开启注解aop 自动产生代理--> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> <!--扫描包--> <context:component-scan base-package="aop.*,AnnoAspect.*"></context:component-scan> <!-- 在配置文件中开启注解的AOP的开发============ --> <aop:aspectj-autoproxy/> <!-- 配置目标类================ --> <!--<bean id="productDao" class="aop.dao.ProductDaoImpl"> </bean>--> <!-- 配置切面类================ --> <bean id="myAspect" class="aop.Aspect.Aspect"></bean> <!--通过aop配置对目标类增强--> <aop:config> <!--切入点 配置哪些类那些方法被增强--> <aop:pointcut id="p1" expression="execution(* aop.dao.ProductDaoImpl.save(..))"></aop:pointcut> <aop:pointcut id="p2" expression="execution(* aop.dao.ProductDaoImpl.delete(..))"></aop:pointcut> <aop:pointcut id="p3" expression="execution(* aop.dao.ProductDaoImpl.update(..))"></aop:pointcut> <aop:pointcut id="p4" expression="execution(* aop.dao.ProductDaoImpl.query(..))"></aop:pointcut> <!--通知 配置通知类--> <aop:aspect ref="myAspect" > <!--前置通知--> <aop:before method="checkPri" pointcut-ref="p1"></aop:before> <aop:after-returning method="logPrint" pointcut-ref="p2" returning="result"></aop:after-returning> <aop:around method="watch" pointcut-ref="p3"></aop:around> <aop:after-throwing method="afterAfterThrowing" pointcut-ref="p4" throwing="ex"></aop:after-throwing> <aop:after method="after" pointcut-ref="p2" ></aop:after> </aop:aspect> </aop:config> </beans>
注解式aop要配置上这句话,pom文件所引入的jar包与上一篇文章一致
posted on 2019-04-14 13:26 Danial7777777 阅读(451) 评论(0) 编辑 收藏 举报