spring配置文件无法使用aop:config标签

spring配置文件无法使用aop:config标签

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

今天学习面向切面编程的时候,在spring配置文件中使用aop:config标签时没有自动提示,但我没管,直接敲上去,启动tomcat的时候发现了异常:
Line 23 in XML document from file [C:\apache-tomcat-8.5.43\wtpwebapps\cs20200802_cloud_note\WEB-INF\classes\conf\spring-aop.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 23; columnNumber: 14; 元素 “aop:config” 的前缀 “aop” 未绑定。


提示:以下是本篇文章正文内容,下面案例可供参考

一、异常出现

异常一、元素 “aop:config” 的前缀 “aop” 未绑定:

org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 23 in XML document from file [C:\apache-tomcat-8.5.43\wtpwebapps\cs20200802_cloud_note\WEB-INF\classes\conf\spring-aop.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 23; columnNumber: 14; 元素 "aop:config" 的前缀 "aop" 未绑定。

异常二、通配符的匹配很全面, 但无法找到元素 ‘aop:config’ 的声明:

org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 25 in XML document from file [C:\apache-tomcat-8.5.43\wtpwebapps\cs20200802_cloud_note\WEB-INF\classes\conf\spring-aop.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 25; columnNumber: 14; cvc-complex-type.2.4.c: 通配符的匹配很全面, 但无法找到元素 'aop:config' 的声明。

二、解决办法

在spring配置文件的 头部beans中添加
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”:

代码如下:

<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:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd     
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

同时满足这两项配置,就可以解决一下两个问题:

  • 元素 “aop:config” 的前缀 “aop” 未绑定

  • 通配符的匹配很全面, 但无法找到元素 ‘aop:config’ 的声明。


总结

这里对文章进行总结:
以上就是今天要讲的内容,本文仅仅介绍了spring配置文件中无法使用 aop:config标签的原因和解决办法。
出错不可怕,怕的是不会找解决方法,有问题找度娘。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
事务类是用来管理数据库事务的,通常包括事务的开始、提交、回滚等方法。下面是一个示例的事务类: ```java import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; public class TransactionManager { private DataSource dataSource; public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; } public void beginTransaction() throws SQLException { Connection connection = dataSource.getConnection(); connection.setAutoCommit(false); } public void commitTransaction() throws SQLException { Connection connection = dataSource.getConnection(); connection.commit(); connection.setAutoCommit(true); connection.close(); } public void rollbackTransaction() throws SQLException { Connection connection = dataSource.getConnection(); connection.rollback(); connection.setAutoCommit(true); connection.close(); } } ``` 上面的事务类包括了三个方法,分别是beginTransaction()、commitTransaction()、rollbackTransaction(),用于开始事务、提交事务、回滚事务。这里的数据源使用了javax.sql.DataSource接口,可以通过Spring进行注入。 接下来,我们可以将事务类使用AspectJ进行AOP编程。首先,在Spring配置文件中加入AspectJ的命名空间: ```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-4.3.xsd" ``` 然后,定义一个切入点,用于在需要进行事务管理的方法上进行拦截: ```xml <aop:config> <aop:pointcut id="transactionalMethods" expression="execution(* com.example.service..*(..))"/> </aop:config> ``` 上面的切入点表达式表示拦截com.example.service包及其子包下的所有方法。 接着,定义一个切面,用于在拦截的方法执行前后进行事务管理: ```xml <aop:config> <aop:pointcut id="transactionalMethods" expression="execution(* com.example.service..*(..))"/> <aop:aspect ref="transactionManager"> <aop:before method="beginTransaction" pointcut-ref="transactionalMethods"/> <aop:after method="commitTransaction" pointcut-ref="transactionalMethods"/> <aop:after-throwing method="rollbackTransaction" throwing="java.sql.SQLException" pointcut-ref="transactionalMethods"/> </aop:aspect> </aop:config> ``` 上面的切面引用了事务管理类TransactionManager,并在拦截的方法执行前后进行了事务管理。其中,before元素表示在方法执行前调用beginTransaction()方法,after元素表示在方法执行后调用commitTransaction()方法,after-throwing元素表示在方法抛出异常时调用rollbackTransaction()方法。 最后,将事务管理类注入到Spring容器中: ```xml <bean id="transactionManager" class="com.example.TransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> ``` 上面的代码将TransactionManager类注入到容器中,并设置了数据源dataSource。这样,在需要进行事务管理的方法中,就可以使用TransactionManager类进行事务管理了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值