spring-aop 由于无法创建后备 PointcutExpression 而无法工作

问题描述 投票:0回答:1

方面

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 注册了切面和服务。然而,AOP 并没有按预期工作。我检查了切入点表达式,它是正确的。包名和类名也正确。但我仍然收到

invalidAbsoluteTypeName
错误。如果我将表达式更改为这个
execution(* dim.service.QuartzServiceImpl..*(..))
,错误消息消失了,但 AOP 仍然无法运行。

jboss wildfly quartz-scheduler aspectj spring-aop
1个回答
0
投票

您在日志中注意到这一点了吗(添加换行符以提高可读性)?

Creating JDK dynamic proxy:
  target source is SingletonTargetSource
  for target object [dim.service.QuartzServiceImpl@51161910]

您正在使用 Spring 的默认代理模式,即,如果您的类实现了一个接口,Spring 将创建一个 JDK 代理来实现该类也实现的所有接口,但扩展这些接口,而不是

*Impl
类本身。 IE。在这种情况下,代理不是 QuartzServiceImpl 的子类,而是
QuartzService
的子类,代理仅将调用传递给其
QuartzServiceImpl
委托。
这解释了为什么你的切入点不匹配。只需将其更改为 

execution(* dim.service.QuartzService.*(..))

within(dim.service.QuartzService)
也可以更改代理模式以直接扩展类,即使它们实现了接口,但实际上,针对接口进行编程比针对特定实现进行编程要干净得多。

© www.soinside.com 2019 - 2024. All rights reserved.