• 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>


  • 相关阅读:
    机器学习到深度学习资料
    安装CentOS 6停在selinux-policy-targeted卡住的问题解决
    U盘安装Ubuntu 16.04出现:Failed to load ldlinux.c32
    Ubuntu 16.04下使用UNetbootin制作的ISO镜像为U盘启动出现:Missing Operating System (mbr.bin)
    为什么Linux的Fdisk分区时First Sector为2048?
    Windows下将ISO镜像制作成U盘启动的工具(U盘启动工具/UltraISO/Rufus/Universal-USB)
    CentOS 6.9安装类型选择(Basic Server/Web Server)
    Java中String与byte[]的转换
    IntelliJ IDEA插件-翻译插件
    Mycat查询时出现:Error Code: 1064. can't find any valid datanode
  • 原文地址:https://www.cnblogs.com/joyous-day/p/6136384.html
Copyright © 2020-2023  润新知