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个回答

aop:config

<aop:aspect id="xmlAspect" ref="xmlAspect">
       **<aop:pointcut expression="execution(public * org.drsoft.business.*.* (..))" id="allBefore"/>**
    <aop:before method="BeforeLog" pointcut-ref="allBefore"/>
</aop:aspect>

/aop:config
你把这个aop:pointcut expression如上面,移动到aop:aspect中间,再试,应该好了。

代码调整,输出了方法名称,根据输出结果,发现是同一个方法执行了多次
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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐