当前位置: 首页 > 知识库问答 >
问题:

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

潘泳
2023-03-14

我的建议是正确执行并预先准备正确的操作,除了执行两次。我希望它只执行一次。应该触发通知的方法只执行一次,因为startTestSuite标题只在日志中打印一次。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
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);  
    }
<?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>
 @Before("execution(void test.ui.tests.Instrumentation.startTestSuite())")
[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

共有1个答案

沈俊晤
2023-03-14

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

 类似资料:
  • 我正在使用SpringAOP创建一个方面。我定义的方面将执行两次。我似乎不明白为什么。我非常感谢任何人对这个问题的意见。 谢谢 //Spring配置 //建议 //建议的方法 //输出 //编辑1:添加了更多的spring配置信息。根本不使用任何Spring AOP注释。我附加了一个调试器,看到aspect/log语句也被执行了两次。所以我怀疑它是否与日志语句打印字符串两次有关。

  • 基本上,这就是我正在做的 1) 将AlarmManager设置为执行广播接收器(BCR) 2) 从BCR启动MyActivity 3)如果我的活动没有打开,请打开屏幕 出于某种原因,我注意到当MyActivity打开时,它的流程如下所示: onCreate/onNewIntent- 我不知道为什么它会马上暂停。我注意到这只发生在屏幕被标志打开时。有人知道为什么会这样吗?有什么办法可以防止这种行为吗

  • 问题内容: 我的活动课在这里: 和相机预览类在这里: 但是,当我测试该类时,似乎首先调用onResume(),然后在1或2秒后再次调用。因此,相机必须再次刷新。如果我根本没有onResume(),则摄像头预览稳定,但是如果我从主屏幕或其他某个应用再次切换到该应用,则会崩溃。我发现onPause()不会影响任何一个。我的代码正确吗?我应该添加/删除哪些内容以使其不会再次刷新并且在应用切换后仍然不会崩

  • 我使用的是aspectj的weaving而不是SpringAOP,因此我的aspectj maven插件如下所示: 我还有另一个方面看起来是这样的: 我需要的是把上面的(集成方面)放在任何其他方面(包括Spring的安全方面)之前,正如您所看到的,我用尝试过(我也用在.aj文件中尝试过),不幸的是,没有成功。

  • 问题内容: 我正在学习Go,并且想尝试goroutine和频道。 这是我的代码: 结果如下: 我不明白为什么我的goroutine永远不会执行。没有输入“进入goroutine”,并且没有任何错误消息。 问题答案: 事实是您的goroutine开始执行,但是在执行任何操作之前就结束了,因为您的程序在打印后立即停止:goroutine的执行与主程序无关,但是将在与程序相同的位置处停止。因此,基本上,

  • 我有一个C++实验室,问题是:用户应该为X输入一个值(X是所持有的测试数)。如果x<15,程序不计算任何东西。如果X在16和30之间,程序应计算C=1.0/10.0*(24a);如果X>30,程序应计算C=0.15(24*a)。我的multiple if代码可以工作,但是当我输入X的值时,方程没有解出。有人知道吗??