【问题标题】:java.lang.NoSuchMethodError: .....EventProcessor: method <init>()V not foundjava.lang.NoSuchMethodError: .....EventProcessor: 方法 <init>()V 未找到
【发布时间】:2014-07-08 14:29:59
【问题描述】:

我的目标是在我所有的命令处理程序的 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

这个方法 init 到底是什么?可能的根本原因是什么?

【问题讨论】:

  • 不知道关于AOP,但看起来插件根本找不到EventProcessor的默认构造函数。
  • 但是插件在编译时成功地在我的类上编织了@Transactional 方面。

标签: java spring scala aspectj


【解决方案1】:

我刚刚找到了东西:)

@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]
      }
    })
  }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-17
    • 2011-01-26
    • 2013-06-27
    相关资源
    最近更新 更多