问题描述
bean.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: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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <!-- 打开spring对annotation的支持 --><context:annotation-config/><!-- 设定spring去那些包中找annotation --><context:component-scan base-package="com.cms"></context:component-scan><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/cms_test" /> <property name="username" value="root" /> <property name="password" value="huhuawen@" /> <!-- 配置连接池的初始值 --> <property name="initialSize" value="1" /> <!-- 配置连接池的最大值 --> <property name="maxActive" value="500" /> <!-- 最大空闲时,当经过一个高峰之后,连接池可以将一些用不到的连接释放,一直减少到maxIdle为止 --> <property name="maxIdle" value="2" /> <!-- 最小空闲时,当连接少于 minIdle时会自动申请一些连接--> <property name="minIdle" value="1" /> <property name="maxWait" value="1000" /></bean><!-- 创建spring的sessionFactory工厂 --><!-- 和hibernate4整合没有提供专门针对Annotion的类,直接在LocalSessionFactoryBean中已经集成 --><bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <!-- 注入数据源 --> <property name="dataSource" value="dataSource"'/> <!-- 设置spring取那个包中查找相应的实体类 --> <property name="packagesToScan" value="com.cms.common.model" /> <property name="hibernateProperties"> <props key="hibernate.dialect">org.hibernate.dialect.MYSQLDialect</props> <props key="hibernate.show_sql">true</props> <props key="hibernate.hbm2ddl.auto">update</props> <props key="hibernate.format_sql">false</props> </property></bean><!-- 配置spring的事务处理 --><!-- 创建事务管理器 --><bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property></bean><!-- 配置AOP,spring是通过AOP来进行事务管理的 --><aop:config><!-- 以下的事务是在DAO中,但是通常都在Service来处理多个业务对象逻辑的关系,注入删除,更新等就会导致数据不完整,所以事务不应该在DAO层处理,而应该在service层处理。 --> <aop:pointcut expression="allMethods" id="execution(* com.cms.common.dao.*.* (..))"/> <!-- 通过advisor来确定具体要加入事务控制的方法 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="allMethods"/> </aop:config> <!-- 配置那些方法要加入事务控制 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <!-- 让所有的方法都加入事务管理,为了提高效率,可以把一些查询之类的方法设置为只读的事务 --> <tx:attributes> <tx:method name="*" propagation="REQUIRED" read-only="true"/> <!-- 以下方法都可能的设计修改的方法。就无法设置为只读 --> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="del*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="save*" propagation="REQUIRED" /> </tx:attributes> </tx:advice></beans>提示的错误说"sessionFactory"附近有错 property没有结束符,希望慧眼帮我看错来。提示的错误如下:Caused by: org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 39 in XML document from class path resource [beans.xml] is invalid; nested exception is org.xml.sax.SAXParseException: Element type "property" must be followed by either attribute specifications, ">" or "/>".at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396)at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:243)at org.springframework.test.context.support.AbstractGenericContextLoader.loadBeanDefinitions(AbstractGenericContextLoader.java:233)at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:117)at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:100)at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:248)at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContextInternal(CacheAwareContextLoaderDelegate.java:64)at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:91)... 31 moreCaused by: org.xml.sax.SAXParseException: Element type "property" must be followed by either attribute specifications, ">" or "/>".at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:174)at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:388)at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1411)at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.seekCloseOfStartTag(XMLDocumentFragmentScannerImpl.java:1391)at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:285)at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2747)at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:807)at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:107)at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:225)at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:283)at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:75)at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:388)... 44 moreTests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.522 sec <<< FAILURE!
解决方案
<!-- 注入数据源 --> <property name="dataSource" value="dataSource"'/> 多了一个单引号
解决方案二:
还有可能是中文注释造成的 你删除试试