【问题标题】:Why isn't my custom Spring aspect triggered?为什么我的自定义 Spring 方面没有被触发?
【发布时间】:2014-07-08 01:56:42
【问题描述】:

我想使用 Spring 4.X 编织以下自定义方面。
(我使用 Scala,但与 Java 中的完全相同)。
我基于这个现有的code,作为 Vaughn Vernon 的 IDDD 书中的样本:

@Aspect
@Component
class EventProcessor @Autowired()(private val eventRepository: EventRepository) {

  @Before("execution(* com.mymainpackage.*.application.commands.*.*(..)")
  def listen() {
    DomainEventPublisher.instance().subscribe(new DomainEventSubscriber[Event] {

      def handleEvent(domainEvent: Event) {
        eventRepository.save(domainEvent)
      }

      def subscribedToEventType = {
        classOf[Event]
      }
    })
  }

}

我的application-context.xml

.........
<context:load-time-weaver/>
<context:annotation-config/>
<context:component-scan
        base-package="........" />

我想用方面编织的典型类/服务包含这种方法:

def handle(event:Event) 

及其对应的接口和实现可以在这个显式包中找到,例如:

com.mymainpackage.myboundedcontext1.application.commands.anestedpackage 

我仔细检查了基础包是否包含自定义方面的类包。

是否有必要在@Before 之外声明@PointCut
示例不这样做...
当然,我使用以下 jar 执行我的代码,允许在运行时“编织”,所以:

-javaagent:/cache/org.springframework/spring-instrument/jars/spring-instrument-4.0.0.RELEASE.jar

我错过了什么“明显”的东西吗?
知道如何解决这个问题吗?

【问题讨论】:

  • 对于初学者,您的切入点缺少结束 )。要使加载时间编织工作,您还需要aop.xml 确保您也拥有它。只有其中声明的方面才会被应用。
  • 感谢您的括号!

标签: java spring scala aop aspectj


【解决方案1】:

正如@M.Deinum 提到的,我忘记了最后一个) 关于我的切入点的匹配器。

此外,我忘记指定 aspectj-autoproxy,以便考虑到我的方面类。
有了这个,如果我希望所有方面类都有自己创建的代理,就不需要声明 aop.xml。

所以我通过像这样将 spring-aop xsd 添加到我的application-context.xml 来指定它(“------->”在这个 xml 中创建一个图例,虽然无效:)):

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:aop="http://www.springframework.org/schema/aop"       ------->That is added
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/data/neo4j
           http://www.springframework.org/schema/data/neo4j/spring-neo4j-2.1.xsd  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd" ---------> That is added >

      .....
      <aop:aspectj-autoproxy/>
      .....

现在一切正常了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多