springAOP注解方式定义切入点报错error at ::0 can't find referenced pointcut

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springAOP注解方式定义切入点报错error at ::0 can't find referenced pointcut相关的知识,希望对你有一定的参考价值。

【说明】

  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>    

 

  

 

以上是关于springAOP注解方式定义切入点报错error at ::0 can't find referenced pointcut的主要内容,如果未能解决你的问题,请参考以下文章

spring中自定义注解

SpringAOP注解报错:java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut se

Spring Aop实现方式(注解和Xml)

Spring Aop实现方式(注解和Xml)

spring实现aop具体步骤

SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)