Spring配置文件application-context.xml报错,请求技术解决方案
解决Spring application-context.xml的XSD引用错误
我来帮你排查这个Spring配置文件的XSD引用问题,这类错误通常和XSD文件的获取、版本匹配或者配置声明有关,试试下面这些解决方案:
1. 修正Schema声明的缺失问题
看你的配置里,已经引入了mvc的命名空间(xmlns:mvc="http://www.springframework.org/schema/mvc"),并且用到了<mvc:annotation-driven />标签,但在xsi:schemaLocation里没有添加对应的mvc XSD地址,这会直接导致IDE无法找到该标签的校验规则,进而可能连带提示其他XSD有问题。同时你还声明了没用的jee命名空间,也可以删掉。
修正后的xsi:schemaLocation应该是这样:
xsi:schemaLocation="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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"
2. 确保Spring依赖版本统一
你配置里用的是org.springframework.orm.hibernate4.LocalSessionFactoryBean,说明你用的是Spring 4.x版本。请检查项目中所有Spring相关的依赖(spring-beans、spring-context、spring-webmvc、spring-orm等)版本完全一致,不要混合使用不同版本的Spring jar包,版本不兼容很容易引发XSD校验错误。
3. 给XSD地址添加版本号
如果IDE无法远程获取无版本号的XSD文件,可以给每个XSD地址加上对应Spring版本的后缀,比如你用Spring 4.3.x的话,就改成:
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"
这样IDE可以直接从本地Maven仓库中找到对应版本的XSD文件,避免网络访问问题。
4. 处理网络或IDE缓存问题
- 如果你的开发环境无法访问外网,IDE就无法下载远程XSD文件。可以手动将Spring jar包中的XSD文件(比如spring-beans.jar里的spring-beans.xsd)添加到IDE的XML Catalog中,关联对应的命名空间URI。
- 尝试清理IDE缓存:Eclipse可以用
Project -> Clean,IDEA则是File -> Invalidate Caches / Restart,然后重新构建项目,让IDE重新加载配置。
修正后的完整配置代码
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- Specifying base package of the Components like Controller, Service, DAO --> <context:component-scan base-package="com.common" /> <!-- Getting Database properties --> <context:property-placeholder location="classpath:application.properties" /> <mvc:annotation-driven /> <!-- View Resolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/" /> <property name="suffix" value=".jsp" /> </bean> <!-- step 1 : DataSource database / Datasource connection pool--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${database.driver}"></property> <property name="url" value="${database.url}"></property> <property name="username" value="${database.user}"></property> <property name="password" value="${database.password}"></property> </bean> <!--step 2 : Hibernate SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="packagesToScan" value="com.common.objects"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> </props> </property> </bean> <!--step 3 : setting Hibernate Transaction manager--> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <bean id = "helloWorld" class = "com.tutorialspoint.HelloWorld"> <property name = "message" value = "Hello World!"/> </bean> </beans>
内容的提问来源于stack exchange,提问作者Karewar Nikhil




