0

我正在使用 axonframework 2.3.1 ,对于应用程序的单元测试,有一个包含一些事件处理程序的Aggregate 类。现在我希望在调用 Aggregate 类中包含的命令处理程序方法之前,我想应用 aop 跟踪 @Before 和 @After 那些处理程序方法。
我正在使用 FixtureConfiguration 接口并将 newGivenWhenThenFixture 应用于聚合类,因为轴突配置类的布线是由轴突框架完成的。
我已经在另一个 xml 文件中配置了 aop 配置,并在运行测试用例之前加载了该 xml 文件。如何将 aop 跟踪与 axon 有线聚合类集成。
谢谢

我在http://www.axonframework.org/axon-2-quickstart-guide/#step1使用了这个例子,在这个例子中我希望我应该能够为 class ToDoEventHandler 每个调用的方法记录之前/之后的跟踪消息。

下面是类似的代码,我在其中编写了一些聚合和方面进行配置。我有一个聚合类

    public class ToDoItem extends AbstractAnnotatedAggregateRoot{

    @AggregateIdentifier
    private String id;

     @CommandHandler
        public ToDoItem(CreateToDoItemCommand command) {
            apply(new ToDoItemCreatedEvent(command.getToDoId(), command.getDescription()));
    }
    @CommandHandler
    public void markCompleted(MarkCompletedCommand command){
        apply(new ToDoItemCompletedEvent(id));      
    }
    public ToDoItem(){

    }

    @EventHandler
    public void on(ToDoItemCreatedEvent event){
        this.id=event.getTodoid();
    }

}

和一个 EventHandler 类

    public class ToDoEventHandler {

    @EventHandler
    public void handle(ToDoItemCreatedEvent event) {
        System.out.println("We are starting a task: "
                + event.getDescription() + " (" + event.getTodoid() + ")");
    }

    @EventHandler
    public void handle(ToDoItemCompletedEvent event) {
        System.out.println("We've completed a task: " + event.getTodoid());
    }

}

spring-axon的配置文件如下

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:axon="http://www.axonframework.org/schema/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                       http://www.axonframework.org/schema/core http://www.axonframework.org/schema/axon-core-2.0.xsd">

<axon:command-bus id="commandBus" />
<axon:event-bus id="eventBus" />

<axon:event-sourcing-repository id="toDoRepository"
    aggregate-type="com.my.axon.ToDoItem" />

<axon:aggregate-command-handler id="toDoItemHandler"
    aggregate-type="com.my.axon.ToDoItem" repository="toDoRepository"
    command-bus="commandBus" />

<axon:filesystem-event-store id="eventStore"
    base-dir="events" />


<bean
    class="org.axonframework.commandhandling.gateway.CommandGatewayFactoryBean">
    <property name="commandBus" ref="commandBus" />
</bean>

<axon:annotation-config />
<bean class="com.my.axon.ToDoEventHandler" />

<bean class="com.my.axon.AopConfigurator"></bean>

现在我希望在 ToDoEventHandler 类之前/之后调用的每个方法我都应该能够在方面之前和之后记录,所以我创建了一个方面并对其进行了配置。

/**
 * This class is used to configure the AOP related beans 
 * instead of doing the entries the beans are configured here and the
 * same effect is achieved using @EnableAspectJAutoProxy annotation. 
 * @author anand.kadhi
 *
 */
@Configuration
@EnableAspectJAutoProxy
public class AopConfigurator {

    @Bean
    public AspectRunner aspectOperation()
    {
        return new AspectRunner();
    }   
}

和方面

    @Aspect 
    public class AspectRunner {

        /**
         * This pointcut will call respective before and after method execution 
         * points
         */
        @Pointcut("execution(* com.my.axon.ToDoEventHandler.*(..))")
        public void logging(){};

        @Before("logging()")
        public void entering(JoinPoint joinPoint)
        {

            System.out.println("After completing Class : "+joinPoint.getTarget().getClass().getName() +" and method : "+joinPoint.getSignature().getName());
        }

        @After("logging()")
        public void exiting(JoinPoint joinPoint)
        {
            System.out.println("After completing Class : "+joinPoint.getTarget().getClass().getName() +" and method : "+joinPoint.getSignature().getName());


        }
    }

并且有一个主类

public class ToDoItemRunner {

private CommandGateway commandGateway;

public ToDoItemRunner(CommandGateway commandGateway) {
    this.commandGateway = commandGateway;
}

public static void main(String[] args) {
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-axon.xml");
    ToDoItemRunner runner = new ToDoItemRunner(applicationContext.getBean(CommandGateway.class));
    runner.run();
}

private void run() {
    final String itemId = UUID.randomUUID().toString();
    commandGateway.send(new CreateToDoItemCommand(itemId, "Need to do this"));
    commandGateway.send(new MarkCompletedCommand(itemId));
}

}

我希望在 ToDoEventHandler 类之前/之后调用我的每个方法都应该能够在方面之前和之后记录。

提前致谢。

4

0 回答 0