li3807 2017-02-01 05:51 采纳率: 0%
浏览 2017

spring aop aspectJ 使用XML配置AOP,但同一个切入方法执行二次的问题

spring aop aspectJ 使用XML配置AOP,只增加了一个切入点,但同一个切入方法执行二次的问题

Spring AOP XML配置文件:
<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">

<bean id="userManager" class="org.drsoft.business.UserManagerImpl">

</bean>

<aop:config>
    <aop:pointcut expression="execution(public * org.drsoft.business.*.* (..))" id="allBefore"/>

    <aop:aspect id="xmlAspect" ref="xmlAspect">
        <aop:before method="BeforeLog" pointcut-ref="allBefore"/>
    </aop:aspect>
</aop:config>

<bean id="xmlAspect" class="org.drsoft.aop.XMLAspect"/>

<bean
    class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator">
</bean>

切面类:
package org.drsoft.aop;

public class XMLAspect {
public void BeforeLog() {
System.out.println("XMLAspect BeforeLog Time=" + System.currentTimeMillis());
}
}
Main方法:
package org.drsoft;

import org.drsoft.business.UserManager;
import org.drsoft.utils.SpringUtils;

public class SpringAOPXMLMain {
public static void main(String[] args) {
UserManager userManager = SpringUtils.getBean("userManager", UserManager.class);

    userManager.signUser();
}

}

输出结果:
XMLAspect BeforeLog Time=1485916445019
XMLAspect BeforeLog Time=1485916480819
UserManagerImpl signUser Timestamp=1485916483706

  • 写回答

2条回答

  • li3807 2017-02-01 06:03
    关注

    代码调整,输出了方法名称,根据输出结果,发现是同一个方法执行了多次
    public void BeforeLog(JoinPoint joinPoint) {
    System.out.println("XMLAspect BeforeLog Time=" + System.currentTimeMillis() + "\tName="
    + joinPoint.getSignature().getName());
    }

    输出结果:
    XMLAspect BeforeLog Time=1485928792439  Name=signUser
    

    XMLAspect BeforeLog Time=1485928792441 Name=signUser
    UserManagerImpl signUser Timestamp=1485928792441

    评论

报告相同问题?

悬赏问题

  • ¥15 Stata 面板数据模型选择
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用