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

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

    评论

报告相同问题?