zoukankan      html  css  js  c++  java
  • AOP注解

    AOP注解

      Ø 实现AOP的注解有

        Ø @Aspect 声明切面

        Ø @Ponitcut 声明公共的切点表达式

        Ø @Before 前置增强

        Ø @AfterReturning 后置增强

        Ø @Around 环绕增强

        Ø @AfterThrowing 异常抛出增强

        Ø @After 最终增强

      前置增强剂后置增强

          DoSomeServiceImpl业务类:    

            @Component("doSome")
            public class DoSomeServiceImpl {
    
                public void doSome(){
                   System.out.println("我是小明======");
                }
    
            }

          DoSomeAspet增强类:

            @Aspect //声明该类为切面
            @Component  //将增强类注入到sprig容器中
            public class DoSomeAspet {
    
                @Pointcut("execution(* com.sp.aspet.*.*(..))")
                public void pointcut(){     //声明切点表达式
                }
              @Before("pointcut()")
              public void before(JoinPoint jp){
              System.out.println("我是前置增强======"+jp.getSignature().getName());
              }

              @AfterReturning("pointcut()")
              public void after(JoinPoint jp){
               System.out.println("我是后置增强======"+jp.getSignature().getName());
              }
            }

          application-aspet.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">
                <!--开启Spring对注解的支持-->
                <context:annotation-config></context:annotation-config>
                <!--提供对注解的包的扫描器-->
                <context:component-scan base-package="com.sp.aspet"></context:component-scan>
                <!--开启Spring对AOP注解的配置-->
                <aop:aspectj-autoproxy/>
            </beans>

          测试: 

          public class Test {
              //AOP注解
              public static void main(String[] args) {
                  ApplicationContext applicationContext=new ClassPathXmlApplicationContext("com/sp/aspet/aspet.xml");
                  DoSomeServiceImpl advice =applicationContext.getBean("doSome",DoSomeServiceImpl.class);
                  advice.doSome();
              }
          }

           结果:

            

      环绕增强

             DoSomeServiceImpl业务类:  

            @Component("doSome")
            public class DoSomeServiceImpl {
                public void doSome(){
                   System.out.println("我是小明======");
                }
            }

          DoSomeAspet增强类:

            @Aspect //声明该类为切面
            @Component  //将增强类注入到sprig容器中
            public class DoSomeAspet {
    
                @Pointcut("execution(* com.sp.aspet.*.*(..))")
                public void pointcut(){     //声明切点表达式
                }
    
              @Around("pointcut()")
             public void around(ProceedingJoinPoint jp)throws Throwable{
             System.out.println("我是环绕前置增强======"+jp.getSignature().getName());
             jp.proceed();
             System.out.println("我是环绕后置增强======");
            }
             }

          application-aspet.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">
                <!--开启Spring对注解的支持-->
                <context:annotation-config></context:annotation-config>
                <!--提供对注解的包的扫描器-->
                <context:component-scan base-package="com.sp.aspet"></context:component-scan>
                <!--开启Spring对AOP注解的配置-->
                <aop:aspectj-autoproxy/>
            </beans>

          测试:    

           public class Test {
              //AOP注解
              public static void main(String[] args) {
                  ApplicationContext applicationContext=new ClassPathXmlApplicationContext("com/sp/aspet/aspet.xml");
                  DoSomeServiceImpl advice =applicationContext.getBean("doSome",DoSomeServiceImpl.class);
                  advice.doSome();
              }
          }

           结果:

            

      异常抛出增强

             DoSomeServiceImpl业务类:     

             @Component("doSome")
            public class DoSomeServiceImpl {
                public void doSome(){
                   int num=5/0;
                   System.out.println("我是小明======");
                }
            }

          DoSomeAspet增强类:     

            @Aspect //声明该类为切面
            @Component  //将增强类注入到sprig容器中
            public class DoSomeAspet {
    
                @Pointcut("execution(* com.sp.aspet.*.*(..))")
                public void pointcut(){     //声明切点表达式
                }
               @AfterThrowing(value = "pointcut()",throwing = "e")
              public void error(JoinPoint jp,Exception e)throws Throwable{
               System.out.println("我是异常抛出增强======"+jp.getClass().getSimpleName());
              }
             }

           application-aspet.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">
                <!--开启Spring对注解的支持-->
                <context:annotation-config></context:annotation-config>
                <!--提供对注解的包的扫描器-->
                <context:component-scan base-package="com.sp.aspet"></context:component-scan>
                <!--开启Spring对AOP注解的配置-->
                <aop:aspectj-autoproxy/>
    
            </beans>

           测试:    

           public class Test {
              //AOP注解
              public static void main(String[] args) {
                  ApplicationContext applicationContext=new ClassPathXmlApplicationContext("com/sp/aspet/aspet.xml");
                  DoSomeServiceImpl advice =applicationContext.getBean("doSome",DoSomeServiceImpl.class);
                  advice.doSome();
              }
          }

           结果:

            

      最终增强   

            DoSomeServiceImpl业务类:

             @Component("doSome")
            public class DoSomeServiceImpl {
                public void doSome(){
                   //int num=5/0;
                   System.out.println("我是小明======");
                }
            }

          DoSomeAspet增强类:    

            @Aspect //声明该类为切面
            @Component  //将增强类注入到sprig容器中
            public class DoSomeAspet {
    
                @Pointcut("execution(* com.sp.aspet.*.*(..))")
                public void pointcut(){     //声明切点表达式
                }
              @After("pointcut()")
              public void after(JoinPoint jp)throws Throwable{
               System.out.println("我是最终增强======"+jp.getSignature() .getName());
              }
            }

          aspet.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">
                <!--开启Spring对注解的支持-->
                <context:annotation-config></context:annotation-config>
                <!--提供对注解的包的扫描器-->
                <context:component-scan base-package="com.sp.aspet"></context:component-scan>
                <!--开启Spring对AOP注解的配置-->
                <aop:aspectj-autoproxy/>
    
            </beans>

          测试:   

           public class Test {
              //AOP注解
              public static void main(String[] args) {
                  ApplicationContext applicationContext=new ClassPathXmlApplicationContext("com/sp/aspet/aspet.xml");
                  DoSomeServiceImpl advice =applicationContext.getBean("doSome",DoSomeServiceImpl.class);
                  advice.doSome();
              }
           }

          结果:

            

  • 相关阅读:
    Eclipse workspace被锁定
    OpenWrt增加软件包
    多核cpu关闭、开启核心
    python基础-元组(tuple)及内置方法
    JS变量+作用域
    JS宣传页项目-综合实战
    JS实现轮播图特效(带二级导航)
    JS DOM属性+JS事件
    JS DOM操作(创建、遍历、获取、操作、删除节点)
    JS中BOM操作知识点
  • 原文地址:https://www.cnblogs.com/wnwn/p/11777616.html
Copyright © 2011-2022 走看看