Spring的AOP常见错误


在学习AOP的时候,遇到错误是在所难免的,当在google中搜索这些错误的时候,发现很多人都是没有仔细推敲,只是人云亦云。这里把遇到的一些错误总结下来,以便以后查阅。

1.  切入点表达式定义错误

[1] 错误详细信息如下,红色标注是错误的关键点。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'IStudentMgr' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut update

    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)

    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)

    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)

    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)

    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)

    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192)

    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)

    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)

    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)

    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)

    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)

[2] 解决办法

这个错误的意思是说切入点定义的语法发生错误,错误的定义如下

      @AfterReturning("com.trs.components.mgr.StudentMgr.update()")

 

正确的定义代码如下

@AfterReturning("execution(* com.trs.components.mgr.StudentMgr.update(..))")

 

2.  切入点参数定义错误

[1] 错误详细信息如下,红色标注是错误的关键点

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'IStudentMgr' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut

    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)

    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)

    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)

    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)

    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)

    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192)

    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)

    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)

    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)

    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)

    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)

[2] 解决办法

产生该错误的原因是我们在使用AfterReturning注解的时候,没有定义返回的参数,但是拦截的方法中缺需要传入一个参数,比如下面的“_result”参数。如果AfterReturing注解拦截的方法需要接收参数,需要在AfterReturning中声明。

错误的代码

@AfterReturning("execution (* com.trs.components.mgr.StudentMgr.update(..))")

public void writeAge(Object _result) {

        System.out.println("result::" + _result);

        System.out.println("我是另外一个切面,年龄::" + 23);

    }

正确的代码(注意下面的红色字体,申明了一个返回名为“_result”的变量):

@AfterReturning(pointcut = "execution (* com.trs.components.mgr.StudentMgr.update*(..))", returning = "_result")

    public void writeAge(Object _result) {

        System.out.println("result::" + _result);

        System.out.println("我是另外一个切面,年龄::" + 23);

    }

相关内容