方面
package dim.aop;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LoggingAspect {
private final Logger LOG = Logger.getLogger(this.getClass());
@Before("execution(* dim.service.QuartzServiceImpl.*(..))")
public void before(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().toShortString();
LOG.error(methodName + " Start");
}
}
字符串
服务
package dim.service;
import javax.annotation.PostConstruct;
import org.springframework.stereotype.Service;
@Service
public class QuartzServiceImpl implements QuartzService {
@PostConstruct
void initJobs() {
}
}
型
applicationContext.xml
<aop:aspectj-autoproxy expose-proxy="true" />
型
日志
44: 2023-12-28 17:12:24,607 DEBUG [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] - Identified candidate component class: VFS resource ["/D:/app/codereadystudio/runtimes/jboss-eap-7.4/standalone/deployments/EmsApp.war/WEB-INF/classes/dim/aop/LoggingAspect.class"]
57: 2023-12-28 17:12:25,124 DEBUG [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] - Identified candidate component class: VFS resource ["/D:/app/codereadystudio/runtimes/jboss-eap-7.4/standalone/deployments/EmsApp.war/WEB-INF/classes/dim/service/QuartzServiceImpl.class"]
79: 2023-12-28 17:12:28,248 DEBUG [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] - Identified candidate component class: VFS resource ["/D:/app/codereadystudio/runtimes/jboss-eap-7.4/standalone/deployments/EmsApp.war/WEB-INF/classes/dim/aop/LoggingAspect.class"]
91: 2023-12-28 17:12:28,726 DEBUG [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] - Identified candidate component class: VFS resource ["/D:/app/codereadystudio/runtimes/jboss-eap-7.4/standalone/deployments/EmsApp.war/WEB-INF/classes/dim/service/QuartzServiceImpl.class"]
663: 2023-12-28 17:12:31,444 DEBUG [org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory] - Found AspectJ method: public void dim.aop.LoggingAspect.before(org.aspectj.lang.JoinPoint)
2550 2023-12-28 17:12:32,203 DEBUG [org.springframework.aop.aspectj.AspectJExpressionPointcut] - Failed to create fallback PointcutExpression
2551: java.lang.IllegalArgumentException: warning no match for this type name: dim.service.QuartzServiceImpl [Xlint:invalidAbsoluteTypeName]
2552 at org.aspectj.weaver.tools.PointcutParser.parsePointcutExpression(PointcutParser.java:301)
4975: 2023-12-28 17:12:49,035 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Creating shared instance of singleton bean 'loggingAspect'
4976: 2023-12-28 17:12:49,035 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Creating instance of bean 'loggingAspect'
4977: 2023-12-28 17:12:49,036 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Eagerly caching bean 'loggingAspect' to allow for resolving potential circular references
4978: 2023-12-28 17:12:49,038 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Finished creating instance of bean 'loggingAspect'
6556: 2023-12-28 17:12:51,144 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Creating shared instance of singleton bean 'quartzServiceImpl'
6557: 2023-12-28 17:12:51,144 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Creating instance of bean 'quartzServiceImpl'
6560: 2023-12-28 17:12:51,150 DEBUG [org.springframework.context.annotation.CommonAnnotationBeanPostProcessor] - Found init method on class [dim.service.QuartzServiceImpl]: void dim.service.QuartzServiceImpl.initJobs()
6561: 2023-12-28 17:12:51,151 DEBUG [org.springframework.context.annotation.CommonAnnotationBeanPostProcessor] - Registered init method on class [dim.service.QuartzServiceImpl]: org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement@ffbad86
6562: 2023-12-28 17:12:51,151 DEBUG [org.springframework.beans.factory.annotation.InjectionMetadata] - Registered injected element on class [dim.service.QuartzServiceImpl]: AutowiredFieldElement for private org.quartz.Scheduler dim.service.QuartzServiceImpl.scheduler
6563: 2023-12-28 17:12:51,151 DEBUG [org.springframework.beans.factory.annotation.InjectionMetadata] - Registered injected element on class [dim.service.QuartzServiceImpl]: AutowiredFieldElement for private dim.service.SystemService dim.service.QuartzServiceImpl.systemService
6564: 2023-12-28 17:12:51,152 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Eagerly caching bean 'quartzServiceImpl' to allow for resolving potential circular references
6565: 2023-12-28 17:12:51,155 DEBUG [org.springframework.beans.factory.annotation.InjectionMetadata] - Processing injected element of bean 'quartzServiceImpl': AutowiredFieldElement for private org.quartz.Scheduler dim.service.QuartzServiceImpl.scheduler
6612: 2023-12-28 17:12:51,304 DEBUG [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] - Autowiring by type from bean name 'quartzServiceImpl' to bean named 'schedulerFactoryBean'
6613: 2023-12-28 17:12:51,304 DEBUG [org.springframework.beans.factory.annotation.InjectionMetadata] - Processing injected element of bean 'quartzServiceImpl': AutowiredFieldElement for private dim.service.SystemService dim.service.QuartzServiceImpl.systemService
6615: 2023-12-28 17:12:51,305 DEBUG [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] - Autowiring by type from bean name 'quartzServiceImpl' to bean named 'systemServiceImpl'
6616: 2023-12-28 17:12:51,305 DEBUG [org.springframework.context.annotation.CommonAnnotationBeanPostProcessor] - Invoking init method on bean 'quartzServiceImpl': void dim.service.QuartzServiceImpl.initJobs()
6640: 2023-12-28 17:12:52,462 DEBUG [org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator] - Creating implicit proxy for bean 'quartzServiceImpl' with 0 common interceptors and 3 specific interceptors
6641: 2023-12-28 17:12:52,462 DEBUG [org.springframework.aop.framework.JdkDynamicAopProxy] - Creating JDK dynamic proxy: target source is SingletonTargetSource for target object [dim.service.QuartzServiceImpl@51161910]
6642: 2023-12-28 17:12:52,465 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Finished creating instance of bean 'quartzServiceImpl'
型
我的服务工作正常,我想添加一个方面。Spring注册了aspect和service。但是AOP并没有像预期的那样工作。我检查了切入点表达式,它是正确的。包和类名也是正确的。但是我仍然得到invalidAbsoluteTypeName
错误。如果我将表达式更改为execution(* dim.service.QuartzServiceImpl..*(..))
,错误消息消失,但AOP仍然不起作用。
1条答案
按热度按时间twh00eeo1#
你在日志中注意到这一点了吗(为了可读性增加了换行符)?
字符串
您使用的是Spring的默认代理模式,即如果您的类实现了一个接口,Spring将创建一个JDK代理实现该类也实现的所有接口,但扩展这些接口,而不是
*Impl
类本身。即在这种情况下,代理不是QuartzServiceImpl
的子类,而是QuartzService
的子类,代理仅将调用传递到其QuartzServiceImpl
委托。这就解释了为什么你的切入点不匹配。简单地把它改为
execution(* dim.service.QuartzService.*(..))
或within(dim.service.QuartzService)
。也可以更改代理模式以直接扩展类,即使它们实现了接口(使用CGLIB代理而不是JDK代理),但实际上,针对接口编程比针对特定实现编程更简单。