[英]AOP Spring Wildcard dont work
我正在尝试使用此教程来学习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接口定义的任何方法的执行:execution(* com.xyz.service.AccountService。*(..))
在服务包中定义的任何方法的执行:执行(。* com.xyz.service(..))
在服务包或子包中定义的任何方法的执行:执行(* com.xyz.service ..(..)。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.