Spring 为什么我的AspectJ建议被执行了两次?

Spring 为什么我的AspectJ建议被执行了两次?,spring,testng,aspectj,Spring,Testng,Aspectj,我的建议是正确执行并执行正确的操作,除了执行两次。我希望它只执行一次。应该触发通知的方法只执行一次,因为StartTestSite标题在日志中只打印一次。bean和上下文是在TestNG类中生成的。我尝试在initSpring方法上使用@BeforeClass和@BeforeSuite标记运行它,结果相同。 进一步背景: 其目的是获取测试套件开始和结束的时间戳,以及单个测试开始和结束的时间戳。最终,我将捕获测试失败时的堆栈跟踪,以便我们能够构建测试自动化最有问题的模式,并允许我们将精力集中在自动

我的建议是正确执行并执行正确的操作,除了执行两次。我希望它只执行一次。应该触发通知的方法只执行一次,因为StartTestSite标题在日志中只打印一次。bean和上下文是在TestNG类中生成的。我尝试在initSpring方法上使用@BeforeClass和@BeforeSuite标记运行它,结果相同。 进一步背景: 其目的是获取测试套件开始和结束的时间戳,以及单个测试开始和结束的时间戳。最终,我将捕获测试失败时的堆栈跟踪,以便我们能够构建测试自动化最有问题的模式,并允许我们将精力集中在自动化中需要修复的更重要的领域,而不是修复琐碎的事情

日志文件

[INFO] 2013-02-11 17:56:07.646-0800 test.ui.tests.BVT.initSpring: context object instantiated
[INFO] 2013-02-11 17:56:07.647-0800 test.ui.tests.BVT.initSpring: Shutdown hook registered
[INFO] 2013-02-11 17:56:07.647-0800 test.ui.tests.BVT.initSpring: Obtained a Instrumentation proxy
[INFO] 2013-02-11 17:56:07.661-0800 instrumentation.dao.implement.TestSuiteDaoImpl.beforeTestSuite: ***************Running Advice: beforeTestSuite: startTestSuite
[INFO] 2013-02-11 17:56:07.948-0800 instrumentation.dao.implement.TestSuiteDaoImpl.beforeTestSuite: ***************Running Advice: beforeTestSuite: startTestSuite
[INFO] 2013-02-11 17:56:07.952-0800 test.ui.tests.InstrumentationImpl.startTestSuite: Going to print the title: startTestSuite
[INFO] 2013-02-11 17:56:07.952-0800 test.ui.helpers.TitleLogger.testTitle: ********************
[INFO] 2013-02-11 17:56:07.952-0800 test.ui.helpers.TitleLogger.testTitle: ** startTestSuite **
[INFO] 2013-02-11 17:56:07.953-0800 test.ui.helpers.TitleLogger.testTitle: ********************
[INFO] 2013-02-11 17:56:07.953-0800 test.ui.tests.BVT.initSpring: Called the Proxy
TestNG类

public class BVT extends SeleniumTest {    
@BeforeSuite
        public void initSpring() {
            titleLog.testTitle("initSpring");
            context = new FileSystemXmlApplicationContext(new String[]{"spring-beans.xml"});
            Assert.assertNotNull(context, "Unable to load spring-beans.xml");
            logger.info("context object instantiated");
            context.registerShutdownHook();
            logger.info("Shutdown hook registered");
            instrument = (Instrumentation) context.getBean("InstrumentationProxy");
            Assert.assertNotNull(instrument, "Unable to create a Instrumentation Proxy");
            logger.info("Obtained a Instrumentation proxy");
            instrument.startTestSuite();
            logger.info("Called the Proxy");
        }
}
代理接口

public interface Instrumentation {
    public void startTestSuite();
}
调用代理方法

@Override
    public void startTestSuite() {
        logger.info("Going to print the title: startTestSuite");
        titleLog.testTitle("startTestSuite");   
    }
忠告

@Override
    @Before("execution(void test.ui.tests.InstrumentationImpl.startTestSuite())")
    public void beforeTestSuite(JoinPoint jp) {
        logger.info("***************Running Advice: " + jp.getSignature().getName());
        DateTimeHelper dth = new DateTimeHelper();
        TestSuite ts = new TestSuite();

        //Get the current time
        Timestamp start = dth.getCurrentSqlTimestamp();

        //Update the object with the starting time
        ts.setTestSuiteStart(start);

        //Commit to the database
        saveTestSuite(ts);  
    }
spring-beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<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"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <context:component-scan base-package="instrumentation.dao.implement" />
    <context:annotation-config />
    <aop:aspectj-autoproxy /> 

    <bean id="basicDS" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${driver}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
        <property name="url" value="${url}" />
    </bean>
    <bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="file:datasource.properties" />
    </bean>
    <bean id="testSuite" class="instrumentation.dao.implement.TestSuiteDaoImpl" autowire="constructor" />
    <bean id="Instrumentation" class="test.ui.tests.InstrumentationImpl" />
    <bean id="InstrumentationProxy" 
                 class="org.springframework.aop.framework.ProxyFactoryBean">
        <aop:scoped-proxy proxy-target-class="false"/>
        <property name="target" ref="Instrumentation" />
    </bean>
以下是添加了jp.getTarget.toString的日志

[INFO] 2013-02-13 14:51:30.258-0800 amazon.omaha.test.ui.tests.BVT.initSpring: context object instantiated
[INFO] 2013-02-13 14:51:30.259-0800 amazon.omaha.test.ui.tests.BVT.initSpring: Shutdown hook registered
[INFO] 2013-02-13 14:51:30.270-0800 amazon.omaha.test.ui.tests.BVT.initSpring: Obtained a Instrumentation proxy
[INFO] 2013-02-13 14:51:30.276-0800 amazon.omaha.test.ui.helpers.TitleLogger.testTitle: *************************************************
[INFO] 2013-02-13 14:51:30.276-0800 amazon.omaha.test.ui.helpers.TitleLogger.testTitle: ** afterTestSuite Advice Method startTestSuite **
[INFO] 2013-02-13 14:51:30.276-0800 amazon.omaha.test.ui.helpers.TitleLogger.testTitle: *************************************************
[INFO] 2013-02-13 14:51:30.276-0800 amazon.omaha.test.ui.helpers.TitleLogger.testTitle: ***********************************************************
[INFO] 2013-02-13 14:51:30.277-0800 amazon.omaha.test.ui.helpers.TitleLogger.testTitle: ** amazon.omaha.test.ui.tests.InstrumentationImpl@cb754f **
[INFO] 2013-02-13 14:51:30.278-0800 amazon.omaha.test.ui.helpers.TitleLogger.testTitle: ***********************************************************
[INFO] 2013-02-13 14:51:30.573-0800 amazon.omaha.test.ui.helpers.TitleLogger.testTitle: *************************************************
[INFO] 2013-02-13 14:51:30.575-0800 amazon.omaha.test.ui.helpers.TitleLogger.testTitle: ** afterTestSuite Advice Method startTestSuite **
[INFO] 2013-02-13 14:51:30.575-0800 amazon.omaha.test.ui.helpers.TitleLogger.testTitle: *************************************************
[INFO] 2013-02-13 14:51:30.576-0800 amazon.omaha.test.ui.helpers.TitleLogger.testTitle: ***********************************************************
[INFO] 2013-02-13 14:51:30.576-0800 amazon.omaha.test.ui.helpers.TitleLogger.testTitle: ** amazon.omaha.test.ui.tests.InstrumentationImpl@cb754f **
[INFO] 2013-02-13 14:51:30.576-0800 amazon.omaha.test.ui.helpers.TitleLogger.testTitle: ***********************************************************
[INFO] 2013-02-13 14:51:30.581-0800 amazon.omaha.test.ui.helpers.TitleLogger.testTitle: ********************
[INFO] 2013-02-13 14:51:30.582-0800 amazon.omaha.test.ui.helpers.TitleLogger.testTitle: ** startTestSuite **
[INFO] 2013-02-13 14:51:30.582-0800 amazon.omaha.test.ui.helpers.TitleLogger.testTitle: ********************
[INFO] 2013-02-13 14:51:30.582-0800 amazon.omaha.test.ui.tests.BVT.initSpring: Called the Proxy

在没有更多上下文的情况下,很难说这里发生了什么,但我的猜测是,不知何故,您的方面注册了两次。

在没有更多上下文的情况下,很难说这里发生了什么,但我的猜测是,不知何故,您的方面注册了两次。

我添加了更多上下文。我不知道这是否有帮助。让我知道您希望看到什么来提供更多的上下文。我不是SpringBean定义方面的专家,但是如果删除该行会发生什么?我注释掉了该行,并且得到了相同的结果/行为。嗯……您最后的注释敲响了警钟。也许您还建议生成CGLib类。在您的建议中,尝试记录建议的目标,我认为jp.getTarget应该可以做到这一点。看看这些是否是生成的类。我添加了一些上下文。我不知道这是否有帮助。让我知道您希望看到什么来提供更多的上下文。我不是SpringBean定义方面的专家,但是如果删除该行会发生什么?我注释掉了该行,并且得到了相同的结果/行为。嗯……您最后的注释敲响了警钟。也许您还建议生成CGLib类。在您的建议中,尝试记录建议的目标,我认为jp.getTarget应该可以做到这一点。看看这些是否是生成的类。我有一个问题-你为什么使用ProxyFactoryBean,你可以直接使用InstrumentationImpl对吗?我有一个问题-你为什么使用ProxyFactoryBean,你可以直接使用InstrumentationImpl对吗?
[INFO] 2013-02-13 14:51:30.258-0800 amazon.omaha.test.ui.tests.BVT.initSpring: context object instantiated
[INFO] 2013-02-13 14:51:30.259-0800 amazon.omaha.test.ui.tests.BVT.initSpring: Shutdown hook registered
[INFO] 2013-02-13 14:51:30.270-0800 amazon.omaha.test.ui.tests.BVT.initSpring: Obtained a Instrumentation proxy
[INFO] 2013-02-13 14:51:30.276-0800 amazon.omaha.test.ui.helpers.TitleLogger.testTitle: *************************************************
[INFO] 2013-02-13 14:51:30.276-0800 amazon.omaha.test.ui.helpers.TitleLogger.testTitle: ** afterTestSuite Advice Method startTestSuite **
[INFO] 2013-02-13 14:51:30.276-0800 amazon.omaha.test.ui.helpers.TitleLogger.testTitle: *************************************************
[INFO] 2013-02-13 14:51:30.276-0800 amazon.omaha.test.ui.helpers.TitleLogger.testTitle: ***********************************************************
[INFO] 2013-02-13 14:51:30.277-0800 amazon.omaha.test.ui.helpers.TitleLogger.testTitle: ** amazon.omaha.test.ui.tests.InstrumentationImpl@cb754f **
[INFO] 2013-02-13 14:51:30.278-0800 amazon.omaha.test.ui.helpers.TitleLogger.testTitle: ***********************************************************
[INFO] 2013-02-13 14:51:30.573-0800 amazon.omaha.test.ui.helpers.TitleLogger.testTitle: *************************************************
[INFO] 2013-02-13 14:51:30.575-0800 amazon.omaha.test.ui.helpers.TitleLogger.testTitle: ** afterTestSuite Advice Method startTestSuite **
[INFO] 2013-02-13 14:51:30.575-0800 amazon.omaha.test.ui.helpers.TitleLogger.testTitle: *************************************************
[INFO] 2013-02-13 14:51:30.576-0800 amazon.omaha.test.ui.helpers.TitleLogger.testTitle: ***********************************************************
[INFO] 2013-02-13 14:51:30.576-0800 amazon.omaha.test.ui.helpers.TitleLogger.testTitle: ** amazon.omaha.test.ui.tests.InstrumentationImpl@cb754f **
[INFO] 2013-02-13 14:51:30.576-0800 amazon.omaha.test.ui.helpers.TitleLogger.testTitle: ***********************************************************
[INFO] 2013-02-13 14:51:30.581-0800 amazon.omaha.test.ui.helpers.TitleLogger.testTitle: ********************
[INFO] 2013-02-13 14:51:30.582-0800 amazon.omaha.test.ui.helpers.TitleLogger.testTitle: ** startTestSuite **
[INFO] 2013-02-13 14:51:30.582-0800 amazon.omaha.test.ui.helpers.TitleLogger.testTitle: ********************
[INFO] 2013-02-13 14:51:30.582-0800 amazon.omaha.test.ui.tests.BVT.initSpring: Called the Proxy