Spring AOP (二) 碰到的一些Exceptions

  1. can't find referenced pointcut 

         Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut

         有關這個問題,我搜索了很多資料,大部分講的是JDK 與Spring 版本以及aspectj的問題。 需要下載與JDK相應的版本,或者是將JDK版本變低。我將現有網上的解決方案都試了,可是我的這個Exception依然存在。後來換了個測試例子試了下,居然沒問題。鬱悶我死了,回頭看前面的例子,發現居然是因爲寫切面時,某個方法的拼寫錯誤導致找不到切點,哎耗了我兩天時間啊,我試過下載不同版本的spring和JDK,aspectj都沒有解決問題。注意,這裏的拼寫錯誤不一定發生在@Pointcut行,其他行同樣導致該異常的產生。

 

其他的Exception大部分由於缺少某包所導致: 一般會有不能找到某個class的提示,

下面的例子將會出現兩個特殊的Exception,我目前還不太明白原因。

 如果 不用interface接口,如下

package test.cmei.spring.common;

public class Monkey {

public void stealPeaches(String name){

System.out.println(" monkey " +name+ " is stealing peaches ");

}


}
package test.cmei.spring.aop;

import org.aspectj.lang.annotation.*;
/**
 * 
 * @author cmei
 *
 */

@Aspect
public class MonkeyGuardian {
	
	@Pointcut("execution(* test.cmei.spring.common.Monkey.stealPeaches(..))")
	public void foundMonkey(){}
	
	@Before(value="foundMokey()")
	public void foundBefore(){
		System.out.println(" Daemon found monkey is in the gardon...");
	}
	
	@AfterReturning("foundMokey()&& args(name,..)")
	public void foundAfter(String name){
		System.out.println(" Daemon cought the moken and asked its name is "+name+"...");
	}

}

 

package test.cmei.spring.common;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MonkeyTest {
	
	public static void main(String[] args){
		ApplicationContext context=new ClassPathXmlApplicationContext("aopconfig.xml");
		Monkey monkey=(Monkey) context.getBean("monkey");
		try{
			monkey.stealPeaches("Lao Sun");
		}catch(Exception e){
			e.printStackTrace();
		}
	}

}
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd" ><bean id="monkey" class="test.cmei.spring.common.Monkey"></bean><bean id="guardian" class="test.cmei.spring.aop.MonkeyGuardian"></bean><aop:aspectj-autoproxy /> </beans> 


將會出現下面兩個特殊的Exception:

2. Cannot proxy target class because CGLIB2 is not available

      Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the class path or specify proxy interfaces.

找到原因了:from spring in action

Spring uses the CGLIB1 library to generate a subclass to your target class. When creating this subclass,
Spring weaves in advice and delegates calls to the subclass to your target class.
When using this type of proxy generation, you need to deploy all of the JAR files

 

3.java.lang.NoClassDefFoundError: org/objectweb/asm/Type

Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/objectweb/asm/Type

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

特別是asm.jar,在spring Dist 裏面已自帶加入了,爲什麼還需要呢?

 

暫時不管了,加入了,不用interface也OK了

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章