天天看點

關于加載Spring加載外部檔案屬性.properties的問題關于加載Spring加載外部檔案屬性.properties的問題

關于加載Spring加載外部檔案屬性.properties的問題

如果你今天遇到和我一樣的問題,那麼我恭喜你,你已經知道答案了。 第一個錯誤: Exception in thread "main" org.springframework.beans.factory. xml.XmlBeanDefinitionStoreException:  Line 1 in XML document from class path resource [beans-prop.xml] is invalid;  nested exception is  org.xml.sax.SAXParseException: White spaces are required between  publicId and systemId. 第二個錯誤:Caused by: org.xml.sax.SAXParseException:  White spaces are required between publicId and systemId.

錯誤展示:

關于加載Spring加載外部檔案屬性.properties的問題關于加載Spring加載外部檔案屬性.properties的問題
關于加載Spring加載外部檔案屬性.properties的問題關于加載Spring加載外部檔案屬性.properties的問題
關于加載Spring加載外部檔案屬性.properties的問題關于加載Spring加載外部檔案屬性.properties的問題

在beans-prop.xml中 <beans xmlns="http://www.springframework.org/schema/beans"

      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  

xmlns:p="http://www.springframework.org/schema/p"//錯誤一

xsi:schemaLocation="http://www.springframework.org/schema/beans  

http://www.springframework.org/schema/tx//錯誤二

http://www.springframework.org/schema/tx/spring-tx.xsd   //錯誤三

http://www.springframework.org/schema/beans/spring-beans.xsd"

xmlns:context="http://www.springframework.org/schema/context"

>

如果是myeclipse8.6自動生成的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"

xsi:schemaLocation="http://www.springframework.org/schema/beans 

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"

>

那麼這個檔案少了什麼東西呢?  的确它少了,Spring-context的版本驅動, 還有context注釋,還有context的聲明,spring-beans.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"

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-3.0.xsd">

以上就是正确的配置檔案

下面是完整的程式: <?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:p="http://www.springframework.org/schema/p"

xsi:schemaLocation="http://www.springframework.org/schema/beans  

http://www.springframework.org/schema/tx  

http://www.springframework.org/schema/tx/spring-tx.xsd   

http://www.springframework.org/schema/beans/spring-beans.xsd"

xmlns:context="http://www.springframework.org/schema/context"

>

<!-- 導入配置檔案 -->

<context:property-placeholder location="classpath:db.properties"/>

  <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

 <property name="user" value="${jdbc.user}"></property>

<property name="password" value="${jdbc.password}"></property>

<property name="driverClass" value="${jdbc.driverClass}"></property>

<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>

</bean>

</beans>

ApplicationContextctx=

new ClassPathXmlApplicationContext("beans-prop.xml");

DataSource datasource=(DataSource) ctx.getBean("dataSource");

System.out.println(datasource.getConnection());

得到結果值 [email protected]

這樣就能得到資料的哈希碼值,也就是連接配接對象,本文就是講述怎麼使用外部檔案, 利用外部檔案的屬性。希望對讀者有所幫助。

繼續閱讀