Spring-AOP load-time weaving on 3rd-party classes

倾然丶 夕夏残阳落幕 提交于 2019-12-07 06:08:37

Spring AOP can only weave into Spring Beans. As your 3rd party target class is not a Spring bean, there is no way to apply an aspect to it. For that purpose you need to use AspectJ which is way more powerful and does not rely on Spring's "AOP lite" implementation based on dynamic proxies.

With AspectJ you have two options:

  • Compile-time weaving (CTW): You can compile aspects into the 3rd party classes and create a new, aspect-enhanced JAR for your dependency.
  • Load-time weaving (LTW): You can weave aspects into the 3rd party classes while they are being loaded at runtime. This takes a few CPU cycles while bootstrapping your application, but spares you from having to re-package the 3rd party JAR.

Edit: Oh, by the way, your pointcut syntax is invalid. You cannot write

@Around("org.elasticsearch.action.search.SearchRequestBuilder.setQuery() && args(queryBuilder)")

Instead you rather need something like

@Around("execution(* org.elasticsearch.action.search.SearchRequestBuilder.setQuery(*)) && args(queryBuilder)")

A method name is not enough, you have to tell the AOP framework that you want to capture its execution() (in AspectJ cou could also capture all its callers via call()). Secondly, you will not capture a method with one QueryBuilder parameter by specifying a method signature setQuery() without any parameters, thus I suggest you use setQuery(*) or, if you want to be even more precise, setQuery(org.elasticsearch.index.query.QueryBuilder). You also need a return type and/or modifier like public in front of the method name or again a joker like *.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!