AOP切面

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_39459229/article/details/81112940

AOP切面(Aspect)

切面: 比如说 事物、权限, 与业务没有任何关系。

 

/**
 * 事物类(Aspect)
 * @author Administrator
 *
 *AOP
 *
 *切面: 比如说  事物、权限, 与业务没有任何关系。
 * 
 */
public class Transcation {

    public void beginTrancation(){
        System.out.println("开启事物");
    }

    public void commit(){
        System.out.println("提交事物");
    }
}

 

目标对象

 

public class QueryRunnerImpl implements QueryRunner {
    /**
     * 跟新
     */
    public int update(String sql){ 
        //前置通知

        System.out.println("保存数据库:"+sql);
        return 1;

         //后置通知
    }  

}   

 

joinpoint(连接点): 指目标对象里面 所有的 方法。

Pointcut(切入点):指要拦截目标对象的 哪些方法?

Advice(通知):指定 被拦截到的方法,做出相应通知。

Weaving(织入):是指把切面应用到目标对象来创建新的代理对象的过程

ApplicationContext.xml配置详解

 

<bean id="queryRunnerImpl" class="dao.QueryRunnerImpl"></bean>


<!-- 事物 -->
<bean id="transcation" class="dao.Transcation"></bean>


<!-- AOP -->
<aop:config>
    <!-- 切入点
    表达式例子:
      住意公共方法的抛行:
      execution (public * *(..))
      住何一-个名字以"set"幵始的方法的执行:execution (* set*(..))
      Accountservice接口定义的任意方法的执行:
      execution ( * com.xyz.service.AccountService.*(..))
      在service包中定义的任意方法的执行:
      execution ( * com.xyz.service.*.*(..) )
      在service包或其子包中定义的任意方法的执行:
      execution ( * com.xyz.service..*.*(..) )
     -->
    <aop:pointcut 
            expression="execution(* dao..*.*(..))" 
            id="point"/>

    <!-- 切面 -->     
    <aop:aspect ref="transcation">
        <!-- 
            前置通知  在目标方法输出之前执行
            method 前置通知的名字
            pointcut-ref 指向切入点表达式
         -->
        <aop:before method="beginTrancation" pointcut-ref="point"/>
        <!-- 
            后置通知  在目标方法输出之后执行
            method 前置通知的名字
            pointcut-ref 指向切入点表达式
         -->
        <aop:after-returning  method="commit" pointcut-ref="point"/>

    </aop:aspect>

</aop:config>

猜你喜欢

转载自blog.csdn.net/qq_39459229/article/details/81112940
今日推荐