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

左师佑图 左师佑图 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
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>


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

...全文

相关推荐

评论 4
收藏 已收藏
分享
返回顶部
左师佑图
Java EE 67,535 成员 225,843 内容