【问题标题】:Could not initialize class org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory on deploying to Servicemix在部署到 Servicemix 时无法初始化类 org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory
【发布时间】:2018-09-29 06:36:55
【问题描述】:

我正在制作一个使用 Spring AOP 使用切入点添加日志记录的功能。 我在 META-INF 中添加了一个 spring 文件夹,其中保存了 beans.xml,其中包含以下代码:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

   <aop:aspectj-autoproxy/>
       <!--<aop:include name="servicesLogger"/> 
   </aop:aspectj-autoproxy> -->

<bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />
<bean id="servicesLogger" class="com.app.services.core.logger.ServicesLogger"/>

ServiceLogger 类位于一个单独的模块中,定义切入点如下:

    package com.app.services.core.logger;
    import org.apache.log4j.Logger;
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.annotation.After;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.springframework.beans.factory.annotation.Configurable;

    @Configurable("servicesLogger")
    @Aspect
    public final class ServicesLogger {

private Logger log = Logger.getLogger(ServicesLogger.class);

private ServicesLogger() {
    super();
}

@Before("execution(* *(..))")
public void beforeLog(JoinPoint point) {
    System.out.println(point.getSignature().getName() + " called...");
    log.info(point.getSignature().getName() + " called...");
}

@After("execution(* *(..))")
public void afterLog(JoinPoint point) {
    System.out.println(point.getSignature().getName() + " called...");
    log.info(point.getSignature().getName() + " called...");
}

@Around("execution(* *(..))")
public void aroundLog(JoinPoint point) {
        System.out.println(point.getSignature().getName() + " called...");
        log.info(point.getSignature().getName() + " called...");
}

}

当我尝试部署包含 beans.xml 文件的模块时,servicemix 无法创建 bean 并引发以下异常:

Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory

我已尝试安装 4 个 jar: - 弹簧核心.jar - spring-aop.jar -aspectjrt.jar - aspectjweaver.jar

进入 servicemix 并尝试通过将它们放在 servicemix 的 lib 文件夹中来提供它,但无济于事。

请指导我在这方面缺少什么。 提前致谢。

【问题讨论】:

  • 你应该精确的弹簧版本。
  • 在 Spring 4 中,该类位于 spring-aop jar 中。其他不相关的东西,你的周围坏了,你必须使用ProceedingJoinPoint,并始终调用proceed(),并将调用结果返回给proceed()。如果应用您的方面,所有方法都将返回 null 并且不会调用任何底层方法,这基本上会破坏您的应用程序!
  • 我正在使用 -spring-core 3.0.0.RELEASE -spring-aop 3.0.0.RELEASE -aspectjrt 1.6.11 -aspectjweaver 1.6.11 会不会是版本问题??
  • @M.Deinum 我使用的是 spring 3,是的,java 文件位于 spring-aop.jar 中。我已将它安装到 karaf 并将其保存在 lib 文件夹中,但没有成功。
  • 你的配置也有问题。 &lt;aop:Aspectj-autoproxy/&gt; 已经处理好了AnnotationAwareAspectJAutoProxyCreator,为什么你的方面@Configurable 我现在在哪里看到你使用该功能,删除它?您的问题可能在于 servicemix/karaf 以及它们如何将事物暴露给类路径。

标签: java spring maven spring-aop apache-servicemix


【解决方案1】:

我能够通过对 osgi/servicemix 进行一些研究来解决这个问题。 Servicemix 提供了一个特性 spring-aspects,它是一个系统捆绑包编号 72。

    features:list

通过这个,您将能够查看 karaf 中的所有可用功能。然后使用以下命令安装该功能。

    features:install spring-aspects

现在安装后,您已经为 servicemix 提供了 aop 功能。现在还需要 2 个罐子:

-aspectjrt.jar

-aspertjweaver.jar

如果您有更高版本的 aspectjweaver,则不需要第一个版本,因为它是 aspectjweaver.jar 的子集

如果您的 .m2 中有上述 jars,只需将它们安装到您的 karaf 中

使用以下命令安装这些:

    osgi:install -s wrap:mvn:org.aspectj/aspectjrt/1.6.11
    osgi:install -s wrap:mvn:org.aspectj/aspectjweaver/1.6.11

在某些情况下可能需要安装一个额外的 jar cglib.jar,可以如下完成:

    osgi:install -s wrap:mvn:cglib/cglib/2.1_3

通过这样做,我能够解决依赖关系并消除上述错误。

希望这对其他人也有帮助。 :)

【讨论】:

    猜你喜欢
    • 2013-03-23
    • 2021-03-01
    • 2021-01-20
    • 2012-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-03
    • 2012-08-01
    相关资源
    最近更新 更多