org.xml.sax.SAXParseException: The prefix "context" for element "context:component-scan" is not boun

前端之家收集整理的这篇文章主要介绍了org.xml.sax.SAXParseException: The prefix "context" for element "context:component-scan" is not boun前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

一、情景描述


今天在写Spring3.0配置文件的时候遇到一个错误提示,如下图所示:


applicationContext.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:aop="http://www.springframework.org/schema/aop"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
	
	<!-- 导入扫描的注解解析器-->
	<context:component-scan base-package="org.oa"/>
	
	<!-- 导入事务的注解解析器  -->
	<tx:annotation-driven transaction-manager="transactionManager"/>
	
	<!-- 通过spring引用hibernate的sessionFactory -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionfactorybean">
		<property name="configLocation">
			<value>classpath:hibernate/hibernate.cfg.xml</value>
		</property>
	</bean>
	
	<!-- 通过spring引用hibernate的事务管理器 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory">
			<ref bean="sessionFactory"/>
		</property>
	</bean>

</beans>


二、分析和排错


起初我以为是MyEclipse的问题,所以没有理会这个错误,于是直接运行Spring3.0+Hibernate3.6的测试用例,结果就出现如下错误

org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from relative location [applicationContext-db.xml]
Offending resource: class path resource [spring/applicationContext.xml]; nested exception is org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 15 in XML document from class path resource [spring/applicationContext-db.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 15; columnNumber: 49; 元素 "context:component-scan" 的前缀 "context" 未绑定。

百度查找原因后才发现,原来是没有在根源素beans中加入关于context的xmlns,所以加入

 xmlns:context="http://www.springframework.org/schema/context"
并且还要告诉xml解析器xmlns是引用哪的schema,所以要在schemaLoaction中加入
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd

便能解决问题了。

修改后的applicationContext.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"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.0.xsd
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
	
	<!-- 导入扫描的注解解析器-->
	<context:component-scan base-package="org.oa.*"/>
	
	<!-- 导入事务的注解解析器  -->
	<tx:annotation-driven transaction-manager="transactionManager"/>
	
	<!-- 通过spring引用hibernate的sessionFactory -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionfactorybean">
		<property name="configLocation">
			<value>classpath:hibernate/hibernate.cfg.xml</value>
		</property>
	</bean>
	
	<!-- 通过spring引用hibernate的事务管理器 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory">
			<ref bean="sessionFactory"/>
		</property>
	</bean>

</beans>
原文链接:https://www.f2er.com/xml/298619.html

猜你在找的XML相关文章