1. 程式人生 > 其它 >python-檔案操作(讀/寫/追加)

python-檔案操作(讀/寫/追加)

1.AOP概念
AOP為Aspect Oriented Programming的縮寫,意為:面向切面程式設計,通過預編譯方式和執行期間動態代理實現程式功能的統一維護的一種技術。
AOP是OOP的延續,是軟體開發中的一個熱點,也是Spring框架中的一個重要內容,是函數語言程式設計的一種衍生範型。利用AOP可以對業務邏輯的各個部分進行隔離,從而使得業務邏輯各部分之間的耦合度降低,提高程式的可重用性,同時提高了開發的效率。

三種實現方式

  1. 使用原生spring API介面
    beforelog.java
public class Log implements MethodBeforeAdvice {
    //method:要執行的目標物件的方法
    //objects:引數(args)
    //object: 目標物件
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println (target.getClass ().getName ()+"的"+method.getName ()+"被執行了");
    }
}

afterlog.java

public class AfterLog implements AfterReturningAdvice {
    //returnValue執行後返回值
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println ("被執行了"+target.getClass ().getName ()+"的"+method.getName ()+"結果為"+returnValue);
    }
}

spring配置檔案

<bean id="userservice" class="com.ji.service.UserServiceImpl"/>
<bean id="log" class="com.ji.log.Log"/>
<bean id="afterlog" class="com.ji.log.AfterLog"/>
           使用原生的api介面
<aop:config>
               切入點:在哪個地方執行     execution(要執行的位置)
          <aop:pointcut id="pointcut" expression="execution(* com.ji.service.UserServiceImpl.*(..))"/>
                執行環繞增強
          <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
          <aop:advisor advice-ref="afterlog" pointcut-ref="pointcut"/>
</aop:config>
  1. 自定義類來說實現aop(主要是切面定義)
    自定義實現類
public class DiyPointCut {
    public void before(){
        System.out.println ("方法執行前");
    }
    public void after(){
        System.out.println ("方法執行後");
    }
}

spring配置檔案

<bean id="diy" class="com.ji.diy.DiyPointCut"/>
<aop:config>
                <aop:aspect ref="diy"> 自定義切面要引入的類;
                   切入點
                    <aop:pointcut id="pointCut" expression="execution(* com.ji.service.UserServiceImpl.*(..))"/>
                   通知
                    <aop:before method="before" pointcut-ref="pointCut"/>
                    <aop:after method="after" pointcut-ref="pointCut"/>
                </aop:aspect>
        </aop:config>
  1. 使用註解實現
    註解實現類
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

//使用註解方式實現aop
@Aspect  //標註這個類是一個切面
public class AnnotationPointCut {
    @Before ("execution(* com.ji.service.UserServiceImpl.*(..))")
    public void before(){
        System.out.println ("方法執行前");
    }
    @After ("execution(* com.ji.service.UserServiceImpl.*(..))")
    public void after(){
        System.out.println ("方法執行後");
    }
    //在環繞增強中可以給定一個引數代表我們要獲取處理切入的點
    @Around ("execution(* com.ji.service.UserServiceImpl.*(..))")
    public void around(ProceedingJoinPoint jp){

        System.out.println ("環繞前");
        //執行方法
        try {
            System.out.println (jp.proceed ());
            System.out.println (jp.getSignature ());
        } catch (Throwable throwable) {
            throwable.printStackTrace ();
        }
        System.out.println ("環繞後");
    }
}

spring配置檔案

 <!--開啟註解支援-->
    <aop:aspectj-autoproxy/>
    <bean id="userservice" class="com.ji.service.UserServiceImpl"/>
    <bean id="annotation" class="com.ji.diy.AnnotationPointCut"/>

總結
註解實現編寫的時候最方便,但後期維護可能會造成困擾,第一種方法需要再寫兩個類,總體個人覺得第二種方法比較好。