Spring aop:发布到Tomcat运行,报错"no declaration can be found for element 'aop:config'"
刚开始接触Spring aop,在*.xml(spring context)加入<aop:config>..</aop:config>,发布到Tomcat启动时报错:no declaration can be found for element 'aop:config', aop:config部分代码如下:
<aop:config>
<aop:pointcut expression="execution(* com.BaseController.*(..))" id="pointcut"/>
<aop:aspect id="testAspect" ref="defaultAOP">
<aop:before method="doBefore" pointcut-ref="pointcut"/>
<aop:after method="doAfter" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
参照网上的解答,做了几个动作:
- 在xml头部补了一个xmlns:aop="http://www.springframework.org/schema/aop",xsi:schemaLocation = 加入 ... http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"
- Maven里加了几个jar包(这里还没用到spring-tx哈,我这个小项目对数据库CRUD暂时没用hibernate等框架,自己写的transaction代码):
- 1)spring-aop-4.2.5.RELEASE.jar, spring-expression-4.2.5.RELEASE.jar,spring-aspects-4.2.5.RELEASE.jar (学习aop前,只用了springmvc简单定义了几个bean, 这三个jar我之前没用到,也就没加入,注:这都是spring-framework官方包里的)
- 2)spring-aopalliance-1.0.0.jar,这个不在Spring4的官方包里,apache-maven官方包里找的
- 3)aspectjweaver-1.6.8.RELEASE.jar,这个直接从maven自带的本地repository里加入的
然后,昨天试了一天都报错,今天啥都没改,竟然正常publish,start,反复试了都ok,不报错,注意:
- 有帖子说 xsi:schemaLocation = ... http://www.springframework.org/schema/aop/spring-aop.xsd 和 http://www.springframework.org/schema/aop/spring-aop-版本号.xsd都可以,没有写版本号不重要,spring会自己识别(?),现在我全都没写,结果是OK的。
-
<aop:pointcut expression="execution(* com.BaseController.*(..))" id="pointcut"/>第一个*必须有,我的com是包,BaseController是类名,你若要aop到包下所有类也可以写成*,这里第二个*是方法名,(..)意思是这个方法几个参数,什么参数类型,比如,可以是(String, ..),
- 另外哈,有帖子说<aop:config>可以(必须?)写成 <aop:config proxy-target-class="false">(或true)原话是 “proxy-target-class属性值决定是基于接口的还是基于类的代理被创建。如果proxy-target-class 属性值被设置为true,那么基于类的代理将起作用(这时需要cglib库)。如果proxy-target-class属值被设置为false或者这个属性被省略,那么标准的JDK 基于接口的代理”, 我入门水平,还不懂哈,但是!!! 我无论写成true还是false(据说false是默认哈),还是根本不写这个proxy-target-class,都不影响运行,都能正确aop,且!!!我从头至尾也没引入过cglib相关的包。So ? OK,搞定,我完全没理解,真相是这样:http://www.iteye.com/problems/94650
最后贴一下完整的*.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <import resource="DatasourcePool.xml" /> <context:component-scan base-package="com"></context:component-scan> <mvc:annotation-driven/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/" /> <property name="suffix" value=".jsp" /> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> </bean>
<mvc:interceptors> <!-- 使用bean定义一个Interceptor,直接定义在mvc:interceptors根下面的Interceptor将拦截所有的请求 --> <mvc:interceptor> <mvc:mapping path="/*.do" /> <mvc:mapping path="/*/*.do" /> <bean class="com.LoginInterceptor" /> </mvc:interceptor> </mvc:interceptors>
<bean id="defaultAOP" class="com.aopDefault" />
<aop:config> <aop:pointcut expression="execution(* com.BaseController.*(..))" id="pointcut"/> <aop:aspect id="testAspect" ref="defaultAOP"> <aop:before method="doBefore" pointcut-ref="pointcut"/> <aop:after method="doAfter" pointcut-ref="pointcut"/> </aop:aspect> </aop:config>
</beans>
谢这几篇帖子哈(包括但不限于这些):
http://blog.csdn.net/a352193394/article/details/7588851
http://www.iteye.com/problems/94650
http://pandonix.iteye.com/blog/336873
http://www.educity.cn/wenda/611777.html
http://www.cnblogs.com/xing901022/p/4264334.html

浙公网安备 33010602011771号