我正在使用axonframework 2.3.1,对于应用程序的单元测试,有一个聚合类,其中包含一些事件处理程序。现在我想要在inovking Aggregate类中包含的commandhandler方法之前我想要应用aop跟踪@Before和@After那些处理程序方法。
我使用FixtureConfiguration接口并将newGivenWhenThenFixture应用于聚合类,作为接线axon配置的类是由axon框架完成的。
我在另一个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类之前/之后,每个名为i的方法应该能够在方面之前和之后进行记录,因此我创建了一个方面并对其进行了配置。
/**
* 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类之前/之后,每个名为i的方法都应该能够在方面之前和之后进行记录。
提前致谢。