歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Spring的AOP常見錯誤

Spring的AOP常見錯誤

日期:2017/3/1 10:40:19   编辑:Linux編程

在學習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);

}

Copyright © Linux教程網 All Rights Reserved