Spring AOP测试出现异常:no declaration can be found for element 'aop:aspectj-autoproxy'.异常信息:org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 11 in XML document from class path resource [beans.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'aop:aspectj-autoproxy'.
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:404)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:342)
……测试环境:
MyEclipse7.5
JDK1.6
JUnit4
spring-framework-2.5.6.SEC01
使用的Jar包有:spring.jar
\lib\aspect\aspectjrt.jar
\lib\aspect\aspectjweaver.jar
\lib\jakarta-commons\commons-logging.jar
\lib\j2ee\common-annotations.jar所有的源文件文件如下:
com.yakoo5.service包下有
     MyInteceptor.java
     PersonService.javacom.yakoo5.service.impl包下有
     PersonServiceBean.javajunit.test包下有
     SpringTest.java根目录下有beans.xml以下是个文件源代码:/**
 * //:MyInteceptor.java 2009-9-1 Benjamin Wu.
 */
package com.yakoo5.service;import org.aspectj.lang.ProceedingJoinPoint;public class MyInteceptor {

public void doBefore(){
System.out.println("前置通知");
}

public void doAfterReturning(String name){
System.out.println("后置通知:"+name);
} public void doAfter(){
System.out.println("最终通知:");
} public void doAfterThrowing(){
System.out.println("异常通知");
}

public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("进入方法");
Object result = pjp.proceed();
System.out.println("退出方法");
return result;
}
}
/**
 * //:PersonService.java 2009-9-1 Benjamin Wu.
 */
package com.yakoo5.service;public interface PersonService { public String getPersonName(Integer id); public void save(String name); public void update(String name, Integer id);}
/**
 * //:PersonServiceBean.java 2009-9-1 Benjamin Wu.
 */
package com.yakoo5.service.impl;import com.yakoo5.service.PersonService;public class PersonServiceBean implements PersonService {
public String getPersonName(Integer id){
return "xxx";
}

public void save(String name){
//throw new RuntimeException("我爱例外");
System.out.println("我是save()方法");
} public void update(String name, Integer id){
System.out.println("我是update()方法");
}
}SpringTest.javapackage junit.test;import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.support.AbstractXmlApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.yakoo5.service.PersonService;
import com.yakoo5.service.impl.PersonServiceBean;public class SpringTest { @BeforeClass
public static void setUpBeforeClass() throws Exception {
} @Test
public void aopTest() {
AbstractXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
"beans.xml");
PersonService ps = (PersonServiceBean) ctx.getBean("personService");
ps.save("hello");
}}beans.xml<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <aop:aspectj-autoproxy/>

<bean id="personService" class="com.yakoo5.service.impl.PersonServiceBean" />
<bean id="aspetBean" class="com.yakoo5.service.MyInteceptor" />
<aop:config>
<aop:aspect id="asp" ref="aspetBean">
<aop:pointcut expression="execution(* com.yakoo5.service.impl.PersonServiceBean.*(..))" id="mycut"/>
<aop:before pointcut-ref="mycut" method="doBefore"/>
</aop:aspect>
</aop:config>
</beans>
麻烦各位帮忙看下到底是哪里出问题了,小弟先谢了!

解决方案 »

  1.   

    感觉楼主需要:aopalliance-×.×.jarspring-aop-×.×.×.jar 
      

  2.   

    问题已解决:
    1.首先,在xsi:schemaLocation中加入以下schemaLocation就可以解决no declaration can be found for element 'aop:aspectj-autoproxy'. 这个问题。
    加入schemaLocation后的结果为:
    xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
               http://www.springframework.org/schema/context/spring-context-2.5.xsd
               http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"2.在1中已解决no declaration can be found for element 'aop:aspectj-autoproxy' 问题,但是测试
    SprintTest.java中的aopTest()方法时,仍会出现以下问题:
    java.lang.ClassCastException: $Proxy5 cannot be cast to com.yakoo5.service.impl.PersonServiceBean
    at junit.test.SpringTest.aopTest(SpringTest.java:25)
        public void aopTest() {
            AbstractXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
                    "beans.xml");
            PersonService ps = (PersonServiceBean) ctx.getBean("personService");
            ps.save("hello");
        }
    解决方法:将PersonServiceBean改为PersonService即可通过测试。原因应该是:Spring在使用AOP创建代理对象时,会首先检查要代理的目标对象是否已经实现了接口,如果已经实现了接口,则通过JDK方式创建代理对象;如果没有实现接口,则通过CGLIB方式创建代理对象。我想:
    在本例中Spring通过JDK方式创建的代理对象$Proxy5是直接实现了PersonService接口。
    通过ctx.getBean("personService")获取的Object对象实际上是生成的代理对象$Proxy5。
    所以,在将ctx.getBean("personService")返回的Object对象强制转换成PersonServiceBean对象时会出现
    java.lang.ClassCastException: $Proxy5 cannot be cast to com.yakoo5.service.impl.PersonServiceBean异常。以上仅是我的个人简介,我没有参考Spring源码,但是原理应该是这样的。
      

  3.   


    这两个包是不需要的,因为使用的是Spring2.5.6的版本,在Spring.jar包里面已经内置了实现AOP所需要的类。