【发布时间】: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-aopjar 中。其他不相关的东西,你的周围坏了,你必须使用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 文件夹中,但没有成功。
-
你的配置也有问题。
<aop:Aspectj-autoproxy/>已经处理好了AnnotationAwareAspectJAutoProxyCreator,为什么你的方面@Configurable我现在在哪里看到你使用该功能,删除它?您的问题可能在于 servicemix/karaf 以及它们如何将事物暴露给类路径。
标签: java spring maven spring-aop apache-servicemix