Spring AOP测试出现异常:no declaration can be found for element 'aop:aspectj-autoproxy'

左师佑图 2009-09-08 07:13:18
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.java

com.yakoo5.service.impl包下有
PersonServiceBean.java

junit.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.java

package 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>


麻烦各位帮忙看下到底是哪里出问题了,小弟先谢了!

...全文
3926 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
musecangying007 2009-09-08
  • 打赏
  • 举报
回复
jf,哈哈,白捡便宜喽
左师佑图 2009-09-08
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 islandrabbit 的回复:]
感觉楼主需要:

aopalliance-×.×.jar

spring-aop-×.×.×.jar
[/Quote]

这两个包是不需要的,因为使用的是Spring2.5.6的版本,在Spring.jar包里面已经内置了实现AOP所需要的类。
左师佑图 2009-09-08
  • 打赏
  • 举报
回复
问题已解决:
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源码,但是原理应该是这样的。
islandrabbit 2009-09-08
  • 打赏
  • 举报
回复
感觉楼主需要:

aopalliance-×.×.jar

spring-aop-×.×.×.jar
内容概要:本文针对无刷直流电机驱动的电子机械制动(EMB)执行器,建立了考虑Stribeck摩擦特性的非线性耦合动力学模型,并在Simulink环境中完成了系统级仿真分析。研究综合集成了电机动力学、齿轮传动机构与制动执行机构的动力学特性,构建了高保真的机电一体化系统模型。重点引入Stribeck摩擦模型以精确描述低速工况下执行器内部存在的静摩擦、粘滞摩擦与库仑摩擦之间的过渡行为,有效提升了系统在启停、反向运动等瞬态过程中的动态响应仿真精度。通过多工况仿真验证了模型的有效性,能够准确反映摩擦引起的爬行、滞后与定位误差等非线性现象,为EMB系统的高性能控制算法设计(如摩擦补偿、滑模控制)与结构优化提供了高可信度的仿真平台。; 适合人群:从事汽车电子制动系统、电机驱动控制、机电系统建模与仿真研究的研究生、科研人员及工程技术人员,需具备扎实的机械动力学、自动控制理论基础和MATLAB/Simulink仿真能力。; 使用场景及目标:①用于高精度电子机械制动系统的设计验证与性能预测;②为消除摩擦非线性影响的先进控制策略(如自适应控制、智能控制)提供精确的被控对象模型;③深入探究Stribeck摩擦等非线性因素对系统动态性能(如响应延迟、稳态误差)的作用机理; 阅读建议:读者应结合提供的Simulink模型文件,深入剖析Stribeck摩擦模块的数学实现与参数辨识方法,建议通过改变输入指令(如阶跃、正弦)和负载条件进行对比仿真,以直观理解非线性摩擦对系统动态特性的影响。

67,535

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧