zoukankan      html  css  js  c++  java
  • spring的AOP(Aspect Oriented Programming)面向切面编程

    程序中需要有日志等需要,若在原本程序加代码会导致代码混乱,不宜维护,解决方法:

    1. 使用代理。
    2. 使用spring的AOP。

    spring的AOP注解方式使用:

    1.加入jar包(com.springsource.org.aopalliance,sapectj.weaver,spring-aop)

     

    2.创建一个切面类(Aspect)

    package com.zhiyou100.kfs.aspects;

    import org.aspectj.lang.JoinPoint;

    import org.aspectj.lang.annotation.After;

    import org.aspectj.lang.annotation.AfterReturning;

    import org.aspectj.lang.annotation.Aspect;

    import org.aspectj.lang.annotation.Before;

    import org.springframework.stereotype.Component;

    @Aspect //切面

    @Component       //bean

    public class LogAspects {

           /*

            *   第一个*:通配符  任意访问修饰符和任意返回值

            *   第二个*:类

            *   第三个*:方法

            *   ..:可变参数

            */

           @Before(value="execution(* com.zhiyou100.kfs.aspects.*.*(..))")

           public void before(JoinPoint joinpoint) {

                  Object[] args = joinpoint.getArgs();//参数

                  String name = joinpoint.getSignature().getName();//方法名字

                  System.out.println("begin,method:"+name);

           }

           @AfterReturning

           @After(value="execution(* com.zhiyou100.kfs.aspects.*.*(..))")       //方法执行return前执行

           public void after(JoinPoint joinpoint) {

                  String name = joinpoint.getSignature().getName();//方法名字

                  System.out.println("end,method:"+name+",result:");

           }

    }

    3.在spring配置文件开启切面注解

           <!-- 包扫描 -->

           <context:component-scan base-package="com.zhiyou100.kfs"/>

           <!-- 开启切面注解 -->

           <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

    4.测试代码

                       ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");

                       TestProxy t=(TestProxy)app.getBean("testProxyImp");

                       t.add(1, 8,1);

    spring的AOPxml方式使用:

    1.创建一个需要被通知的接口和相应实现类

    接口:

    package com.zhiyou100.kfs.aspects.xml;

    public interface TestProxy {

           public void add(int a,int b);

           public int add(int a,int b,int c);

    }

    相应实现类:

    package com.zhiyou100.kfs.aspects.xml;

    public class TestProxyImp implements TestProxy  {

          

           public void add(int a,int b){

                  int sum=a+b;

                  System.out.println(sum);

                  throw new RuntimeException("运行时异常");

           }

          

           public int add(int a,int b,int c){

                  int sum=a+b+c;

                  System.out.println(sum);

                  return sum;

           }

    }

    2.在spring配置文件中配置。

    <?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: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-4.2.xsd">

           <!-- 定义被通知的程序类(可以用注解) -->

           <bean id="testProxyImp" class="com.zhiyou100.kfs.aspects.xml.TestProxyImp"></bean>

           <!-- 定义切面类的bean -->

           <bean id="logAspect" class="com.zhiyou100.kfs.aspects.xml.LogAspects"></bean>

           <!-- 配置切面 -->

           <aop:config>

                  <!-- 定义表达式:切点 -->

                  <aop:pointcut expression="execution(* com.zhiyou100.kfs.aspects.xml.*.*(..))" id="pointcut"/>

                  <!-- 定义切面 -->

                  <aop:aspect ref="logAspect">

                         <!-- 定义前置通知 -->

                         <aop:before method="before" pointcut-ref="pointcut"/>

                         <!-- 定义后置通知 -->

                         <aop:after method="after" pointcut-ref="pointcut"/>

                         <!-- 定义返回通知 -->

                         <aop:after-returning method="cc" pointcut-ref="pointcut" returning="result"/>

                         <!-- 定义异常通知 -->

                         <aop:after-throwing method="e" pointcut-ref="pointcut" throwing="e"/>

                  </aop:aspect>

           </aop:config>

    </beans>

    3.最后去测试

  • 相关阅读:
    C# WinForm多线程(一)Thread类库
    ASP.NET执行循序
    SQLSERVER2014的内存优化表
    C# 5.0 Async函数的提示和技巧
    WPF 绑定
    使用 Cordova+Visual Studio 创建跨平台移动应用(3)
    使用 Cordova+Visual Studio 创建跨平台移动应用(2)
    使用 WPF 创建预加载控件
    A WPF/MVVM Countdown Timer
    使用WPF创建无边框窗体
  • 原文地址:https://www.cnblogs.com/kfsrex/p/11494621.html
Copyright © 2011-2022 走看看