zoukankan      html  css  js  c++  java
  • spring AOP的配置

    我们通常会使用spring框架,使用配置xml文件或者注解方式来进行记录日志等操作

    注解方式本人尚未学习,在这里仅仅记录使用xml文件时遇到的问题

    beans 的头部需要加上

    <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-2.5.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

    </bean>

    如果你的被代理对象方法有两个参数,而代理对象中的方法只需要一个参数,那么需要在xml文件中进行配置,不然会报错

    错误信息:Pointcut is malformed: error at ::0 formal unbound in pointcut

    被代理对象方法

    public void login(String name,String password){
    System.out.println(name + "登录,密码:" + password);
    }

    代理对象

    public void loginBefore(String name){
    System.out.println(name + "登录,已进行安全验证");
    }

    public void loginAfter(String name){
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    System.out.println(name + "用户,登录时间" + sdf.format(new Date()));
    }

    对应配置文件

    <aop:config>
    <aop:pointcut expression="execution(* com.spring.homework1.UserLogin.*(..)) and args(name,..)"
    id="userLoginPointCut" />
    <aop:aspect ref="userLoginProxy">
    <aop:before method="loginBefore" pointcut-ref="userLoginPointCut" arg-names="name"/>
    <aop:after method="loginAfter" pointcut-ref="userLoginPointCut" /><!-- 当然这里不用写也是可以执行的,亲测可用 -->

    </aop:aspect>
    </aop:config>

  • 相关阅读:
    将才和帅才之的区别
    百胜集团XX:BPM实现业务流程全过程无缝链接(案例)
    心、肝、脾、肺、肾五脏解说+ 五脏六腑的作用
    人体的五行属性
    易经卦的通例
    《孙子兵法》中的企业领导艺术和方法
    五行盘谱
    大容量高并发性服务器组的技术解析
    中华哲学的领导艺术
    如何在WINDOW环境下搭建ActivateMQ和zookeeper集群环境
  • 原文地址:https://www.cnblogs.com/weiki/p/4129257.html
Copyright © 2011-2022 走看看