【问题标题】:How to create AOP interceptors for Mule classes?如何为 Mule 类创建 AOP 拦截器?
【发布时间】:2014-09-30 23:58:19
【问题描述】:

这是我迄今为止尝试过的,我的拦截器没有被触发(我的日志中没有“TATATA”):

我的拦截器 AopLoggingInterceptor.java :

package fr.mycompany.bus.flow.reco.ani.custom.interceptor;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

@Aspect
public class AopLoggingInterceptor {

    @Around("execution(* org.mule.api.transport.MessageReceiver.routeMessage(org.mule.api.MuleMessage))")   
    public Object addMonitor(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("TATATA Before");
        Object object = pjp.proceed();
        System.out.println("TATATA After");
        return object;
    }   
}

META-INF/aop.xml:

<aspectj>

    <aspects>
        <aspect name="fr.mycompany.bus.flow.reco.ani.custom.interceptor.AopLoggingInterceptor" />
    </aspects>

    <weaver options="-verbose">
        <!-- Weave types that are within the org.mule.* packages. -->
        <include within="org.mule.*" />
    </weaver>
</aspectj>

我的 Mule/Spring 配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<mule >
<spring:beans>
    <context:component-scan base-package="fr.mycompany.bus" />
    <context:annotation-config />
    <aop:aspectj-autoproxy />

    <!-- Aspect -->
    <spring:bean name="aopLoggingInterceptor" class="fr.mycompany.bus.flow.reco.ani.custom.interceptor.AopLoggingInterceptor" />

</spring:beans>
</mule>

我的 Mule 文件配置由一个流和一个入站端点、2 个出站端点、记录器和转换器组成(经过广泛测试的有效流)。

虚拟机参数:

-XX:PermSize=128M -XX:MaxPermSize=256M -javaagent:D:\path\to\mule\opt\aspectjweaver-1.6.11.jar

从 Eclipse 中开始的 mule 文件中提取显示编织已创建:

[MuleApplicationClassLoader@2934847] info AspectJ Weaver Version 1.6.11 built on Tuesday Mar 15, 2011 at 15:31:04 GMT
[MuleApplicationClassLoader@2934847] info register classloader org.mule.module.launcher.MuleApplicationClassLoader@2934847
[MuleApplicationClassLoader@2934847] info using configuration /D:/BusToolBox/workspaces/dev/.mule/apps/bus-esb-mrc-reco-ani/classes/META-INF/aop.xml
[MuleApplicationClassLoader@2934847] info register aspect fr.mycompany.bus.flow.reco.ani.custom.interceptor.AopLoggingInterceptor

编辑

它适用于我项目中包含的类,但不适用于 mule 类: [MuleApplicationClassLoader@6ad5934d] debug generating class 'fr.mycompany.bus.flow.reco.ani.custom.transformer.CustomerDetailToSiebelRecoAniOutputTransformer$AjcClosure1'

编辑 2

这是我能得到的最好结果(通过使用&lt;context:load-time-weaver /&gt;),加载过程试图寻找由不同类加载器加载的更多类,但结果是:

ERROR 2014-08-08 16:00:46,802 [main] org.mule.module.launcher.application.DefaultMuleApplication: null java.lang.IllegalStateException: ClassLoader [org.mule.module.launcher.MuleApplicationClassLoader] does NOT provide an 'addTransformer(ClassFileTransformer)' method. Specify a custom LoadTimeWeaver or start your Java virtual machine with Spring's agent: -javaagent:org.springframework.instrument.jar

如果我尝试使用 spring-instrument-3.2.1.RELEASE.jar,我会得到与以前相同的结果(只看到主类加载器)。这是否意味着骡子没有希望了?

【问题讨论】:

  • 尝试一下:从 Mule/Spring 配置文件中删除所有内容(拦截器、自动代理、加载时间编织器等),将 aop.xml 中的 weaver 属性更改为 &lt;weaver options="-debug -verbose -showWeaveInfo" /&gt; 并运行-javaagent 指向 aspectjweaver-1.6.11.jar 的应用程序。
  • 我的拦截模式仅在“@Around”注释中声明,因此如果没有它,它会尝试编织他可以到达的类集(没有 mule 类)而没有成功。

标签: spring mule aspectj spring-aop


【解决方案1】:

在使用 Spring AOP 时有一些非常重要的事情。在spring aop documentation 中声明:

使用最简单的方法。 Spring AOP 比使用简单 完整的 AspectJ,因为不需要引入 AspectJ 编译器/编织器进入您的开发和构建过程。如果你 只需要通知 Spring bean 上操作的执行,然后 Spring AOP 是正确的选择。 如果您需要建议对象不要 由 Spring 容器管理(例如典型的域对象), 那么您将需要使用 AspectJ。您还需要使用 AspectJ 如果您想建议除了简单方法执行之外的连接点 (例如,字段获取或设置连接点等)。

因此,如果您希望为 MessageReceiver 方法调用调用 AopLoggingInterceptor,那么这是行不通的,因为 MessageReceiver 对象不是由 Spring 容器管理的。 Spring 容器不会“看到”这个对象。

换句话说,Spring-AOP 不能向任何不是由 Spring 工厂创建的东西添加方面。我在这里找到了statement

【讨论】:

  • "如果您需要通知不受 Spring 容器管理的对象(例如通常的域对象),那么您将需要使用 AspectJ"。这正是我正在做的,我正在使用 AspectJ,所以我不明白你的意思。
【解决方案2】:

查看this example 以了解如何使用 Mule 和 Spring AOP。该示例展示了如何为组件调用 Around 通知,但对于拦截器应该类似。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-10
    • 2015-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多