我的目标是在所有命令处理程序的handle
方法上编织一些自定义方面。
我的自定义方面:
@Aspect
@Component
class EventProcessor @Autowired()(private val eventRepository: EventRepository) {
@Before("execution(* com.mypackage.*.application.commands.*.*(..))")
def listen() {
DomainEventPublisher.instance().subscribe(new DomainEventSubscriber[Event] {
def handleEvent(domainEvent: Event) {
eventRepository.save(domainEvent)
}
def subscribedToEventType = {
classOf[Event]
}
})
}
}
commandHandler
的示例:trait CommentBlog {
def handle(command: MyCommand): ValidationNel[Failure, Unit]
}
在运行时编织自定义方面时,整体效果很好。
对于生产,我希望它在编译时进行编织,因此我使用了出色的plugin来实现它。
但是,运行时出现此错误导致
NoAspectBoundException
:java.lang.NoSuchMethodError: .....EventProcessor: method <init>()V not found
这个方法到底是什么?根本原因可能是什么?
最佳答案
我刚刚找到了东西:)
@Landei提出时,未找到默认构造函数,因为未定义!
在Scala中:class EventProcessor @Autowired()(private val eventRepository: EventRepository) {
创建一个接受EventRepository
的单参构造函数。
所以我的解决方案是:
@Aspect
class EventProcessor {
@Before("execution(* com.mypackage.*.application.commands.*.*(..))")
def listen() {
DomainEventPublisher.instance().subscribe(new DomainEventSubscriber[Event] {
def handleEvent(domainEvent: Event) {
val eventRepository = SpringContext.ctx.getBean("eventRepository", classOf[EventRepository])
eventRepository.save(domainEvent)
}
def subscribedToEventType = {
classOf[Event]
}
})
}
}
关于java - java.lang.NoSuchMethodError:…EventProcessor:未找到方法<init>()V,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24634472/