关于加载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.
错误展示:
在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());
得到结果值
com.mchange.v2.c3p0.impl.NewProxyConnection@167d7fb
这样就能得到数据的哈希码值,也就是连接对象,本文就是讲述怎么使用外部文件,
利用外部文件的属性。希望对读者有所帮助。