zoukankan      html  css  js  c++  java
  • aop面向切面编程

       1)先创建一个业务逻辑类,放在cn.hd.service包下:

    @Service

    publicclass UserService {

    publicvoid save(){

       //System.out.println("写日志:开始保存");

       System.out.println("写保存的业务逻辑的代码...");

       //System.out.println("写日志:结束保存");

       }

    publicvoid update(){

       //System.out.println("写日志:开始保存");

       System.out.println("写修改的业务逻辑的代码...");

       //System.out.println("写日志:结束保存");

       }

    }

      2)再创建一个日志类,放在cn.hd.commons包下:

    package cn.hd.commons;

    publicclass LogUtils {

    publicvoid beforeMethod(){

       System.out.println("写日志:开始保存");

       }

    publicvoid afterMehtod(){

       System.out.println("写日志:结束保存");

       }

    }

    3)配置文件中配置AOP

    <beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:p="http://www.springframework.org/schema/p"

    xmlns:context="http://www.springframework.org/schema/context"

    xmlns:aop="http://www.springframework.org/schema/aop"

    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd

    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd

    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">

      <context:component-scan base-package="cn.hd.service"></context:component-scan>

      <!-- 日志类 -->

      <bean id="logUtils" class="cn.hd.commons.LogUtils"></bean>

       <!-- AOP的配置 -->

       <aop:config>

         <!-- 定义一个切面,ref引用的切面类是上面配置的bean的日志类logUtils -->

          <aop:aspect ref="logUtils">

          <!-- 定义一个切入点, * cn.h

    d.service.*.*(..):表示cn.hd.service包下的任意类任意方法作为切入点。第一个星号表示方法任意返回值-->

             <aop:pointcut expression="execution(* cn.hd.service.*.*(..))" id="logPointPut"/>

            <!-- 前置通知:表示调用切入点的方法之前,自动去通知(调用)切面中的beforeMethod方法 -->

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

                    <!-- 后置通知:表示调用切入点的方法之后,自动去通知(调用)切面中的afterMehtod方法 -->

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

          </aop:aspect>

       </aop:config>

    </beans>


  • 相关阅读:
    经验谈 论前端架构的重要性
    论 Angular的混乱
    DTW 算法(转)
    软件提高发射功率原理
    (转)LSI SAS 1068E Raid CentOS 5.5 安装实例浪潮NF5220系列 分类: linux
    聚类算法总结
    信号相似性的描述
    python科学计算整理
    一个无线通信类投稿的期刊list
    2012年Elsevier旗下Computer Science期刊最新SCI影响因子排名
  • 原文地址:https://www.cnblogs.com/joyous-day/p/6136384.html
Copyright © 2011-2022 走看看