日期:2014-05-20  浏览次数:20885 次

Spring 环境搭建错误
我的beans.xml配置如下
测试时老是报如下错误
Line 17 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 'context:component-scan'.
在线等答案....
<?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: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="cn.edu.nupt.bean.product"/>
  <context:property-placeholder location="classpath:jdbc.properties"/>
  
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" >
  <property name="driverClassName" value="${driverClassName}" />
  <property name="url" value="${url}" />
  <property name="userName" value="${userName}" />
  <property name="password" value="${password}" />
  <property name="initialSize" value="${initialSize}" />
  <property name="maxActive" value="${maxActive}" />
  <!-- collaborators and configuration for this bean go here -->
  </bean>

  <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  <property name="dataSource" ref="dataSource"/>
  <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
  <property name="loadTimeWeaver">
  <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
  </property>
  </bean>

  <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
  <property name="entityManagerFactory" ref="entityManagerFactory"/>
  </bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

------解决方案--------------------
加上红色的
<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"
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">