今天在熟悉Spring aop的面向切面编程时,使用匹配参数方式写的代码报错:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.config.internalTransactionAdvisor': Cannot resolve reference to bean 'org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0' while setting bean property 'transactionAttributeSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0': Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: Pointcut is not well-formed: expecting '(' at character position 0
matchArgs
^
根据报错是说我的切面方法没有括号().解决方法:把切面类中的@Before("matchArgs()")修改成@Before("matchArgs()")就可以了。代码如下:
package com.tfq.springaop.config;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.tfq.springaop.service.AuthService;
/**
*
* @description 参数匹配
* @Pointcut("within(com.tfq.springaop.service.impl.*)&&args(Long)")
* //匹配第一个参数为Long型的方法
* @Pointcut("within(com.imooc.*)&&args(Long,..)")
* @author tangfq;
* @version 2020年7月28日 下午2:30:44
*
**/
@Aspect
@Component
public class ArgsAspectConfig {
@Autowired
AuthService authService;
@Pointcut("args(Long) && within(com.tfq.springaop.service.impl.*)")
public void matchArgs(){
}
@Before("matchArgs()")
public void before(){
System.out.println("##before");
authService.checkAccess();
}
}
注意:@Before的matchArgs(),不要忘记写方法括号了。