【说明】
1、使用spring版本:4.0.4
2、springAOP相关依赖包:
1)aopalliance-1.0.jar
2)aspectjweaver-1.8.9.jar
3)aspectjrt-1.8.9.jar
3、分析:
1)当切面类的方法上通知注解直接输入切入点表达式时,没有报错。切面类代码如下:
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.hibernate.Transaction;
import org.springframework.stereotype.Component; @Component("myTransaction")
@Aspect
public class MyTransaction extends HibernateUtil{ private Transaction transaction; @Before("execution(* net.fifteenho.spring.aop.annotation.PersonDaoImpl.*(..))")
public void beginTransaction() {
transaction = sessionFactory.getCurrentSession().beginTransaction();
} @AfterReturning("execution(* net.fifteenho.spring.aop.annotation.PersonDaoImpl.*(..))")
public void commit() {
transaction.commit();
}
}
2)当切面类的方法上通知注解引入定义的切入点,报错error at ::0 can't find referenced pointcut。切面类代码如下:
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.hibernate.Transaction;
import org.springframework.stereotype.Component; @Component("myTransaction")
@Aspect
public class MyTransaction extends HibernateUtil{ @Pointcut("execution(* net.fifteenho.spring.aop.annotation.PersonDaoImpl.*(..))")
private void mypc(){} private Transaction transaction; @Before("mypc()")
public void beginTransaction() {
transaction = sessionFactory.getCurrentSession().beginTransaction();
} @AfterReturning("mypc()")
public void commit() {
transaction.commit();
}
}
4、本人解决方法:aspectjweaver.jar替换较新的aspectjweaver-1.8.9.jar
5、报错原因:springAOP依赖jar版本过低。
【springAOP相关依赖包下载maven】
<!-- https://mvnrepository.com/artifact/aopalliance/aopalliance -->
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.9</version>
</dependency>