我正在使用 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: - spring-core.jar - spring-aop.jar -aspectjrt.jar -aspectjweaver.jar
进入servicemix并尝试通过将它们放入servicemix的lib文件夹中来提供它,但无济于事。
请指导我,我在这方面缺少什么。 提前致谢。
最佳答案
我通过对 osgi/servicemix 进行一些研究解决了这个问题。 Servicemix 提供了一个 spring-aspects 功能,系统 bundle 编号为 72。
features:list
通过此您将能够查看 karaf 中的所有可用功能。然后使用以下命令安装该功能。
features:install spring-aspects
安装完毕后,您已经向 servicemix 提供了 aop 功能。现在还需要 2 个 jar :
-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
通过这样做,我能够解决依赖关系并消除上述错误。
希望这对其他人也有帮助。 :)
关于java - 部署到 Servicemix 时无法初始化类 org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24489430/