Java AOP Spring通配符不起作用

Java AOP Spring通配符不起作用,java,spring,spring-mvc,aop,spring-aop,Java,Spring,Spring Mvc,Aop,Spring Aop,我正在尝试使用本教程学习AOP: 我使用的不是XML,而是注释 当我使用通配符而不是直接单词时,就会出现问题 此代码非常有效: import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component; @Aspect @Component public class LoggingAspect {

我正在尝试使用本教程学习AOP:

我使用的不是XML,而是注释

当我使用通配符而不是直接单词时,就会出现问题

此代码非常有效:

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LoggingAspect {

    @Before("execution(public String  get*())")
    public void loggingAdvice(){
        System.out.println("Advice run. Get Method Called");
    }
此代码不起作用:

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LoggingAspect {

    @Before("execution(* *  get*())")
    public void loggingAdvice(){
        System.out.println("Advice run. Get Method Called");
    }
这是主要的:

@Component
public class AopMain {
    private final static String xmlPath = "file:src/main/webapp/WEB-INF/spring/context.xml";

    public static void main(String[] args) throws IOException {
        ApplicationContext ctx = new ClassPathXmlApplicationContext(xmlPath);
        ShapeService shapeService = ctx.getBean("shapeService", ShapeService.class);
        System.out.println(shapeService.getCircle().getName());}
}
我得到的错误是:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.handler.MappedInterceptor#0': Cannot create inner bean '(inner bean)' of type [org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor] while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)': Cannot resolve reference to bean 'org.springframework.format.support.FormattingConversionServiceFactoryBean#0' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.format.support.FormattingConversionServiceFactoryBean#0': Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: Pointcut is not well-formed: expecting '(' at character position 15
execution(* *  get*())
               ^^^
有人知道它为什么不起作用吗?

试试这个:

@Before("execution(*  get*())")
public void loggingAdvice(){
    System.out.println("Advice run. Get Method Called");
}
一个星号太多

应该是

@Before("execution(*  get*())")

请参阅下面的解释。这将使您更好地了解AOP中的通配符: 要么你必须使用*或者。。在get(*)或get(…)中

AccountService接口定义的任何方法的执行: 执行(*com.xyz.service.AccountService.*(..)

执行服务包中定义的任何方法: 执行(*com.xyz.service.(.)

执行服务包或子包中定义的任何方法:
执行(*com.xyz.service…(..)

我已经测试了这个。它起作用了。切入点是100%正确的。可能问题是因为实际上您正在将此方面附加到所有bean(包括spring服务和其他),然后发生了一些不可预知的事情。我看不到配置。为了简化案例,请尝试将包限制为您自己的执行(*com.xyz.service.*.get*)以执行
execution(*