spring 配置文件中读取不到properties文件
在spring 配置文件读数据库参数的properties文件 但是读不到1.spring的配置文件
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:jdbc.properties</value>
</property>
</bean>
<!-- ***************本地MySql配置数据源************** -->
<bean id="mySqlDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="username"><value>${jdbc.user}</value></property>
<property name="password"><value>${jdbc.password}</value></property>
<property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>
<property name="url"><value>${jdbc.url}</value></property>
</bean>
2.jdbc.properties
jdbc.url=jdbc:mysql://localhost:3306/cp?useUnicode=true&charaterEncoding=utf-8
jdbc.user=root
jdbc.password=123456
jdbc.driverClass=com.mysql.jdbc.Driver
3.cp\WebRoot\WEB-INF\classes下有jdbc.properties文件
4.报错:
org.springframework.transaction.CannotCreateTransactionException: Could not open JDBC Connection for transaction; nested exception is java.sql.SQLException: No suitable driver found for ${jdbc.url}
5.结论把
${jdbc.url}当成字符串了 并未读到properties里的数据 spring读不到properties --------------------编程问答-------------------- <property name="url"><value>${jdbc.url}</value></property>
你把这里面的值全部换成配置文件里对应的 如果再没出问题 那果然就是配置文件的问题了
话说你把配置文件放哪了 --------------------编程问答-------------------- bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:jdbc.properties</value>*号去掉
</list>少一个结束标志
</property>
</bean> --------------------编程问答-------------------- 加了</list>
<value>classpath*:jdbc.properties</value>*号去掉了还是不行
而且把这里面的值全部换成配置文件里对应的就可以运行
但是${}方法取不到值
--------------------编程问答-------------------- up up up --------------------编程问答-------------------- 改为<context:property-placeholder location="jdbc.properties" />看看吧 --------------------编程问答--------------------
<context:property-placeholder location="classpath:jdbc.properties" />
楼上正解~保险起见,加一个classpath --------------------编程问答-------------------- <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:jdbc.properties</value>
</property>
</bean>
改为
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean> --------------------编程问答-------------------- <bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
通过properties文件,获取driverClassName值
<value>${driverName}</value>
</property>
<property name="url" value="${url}"></property>
<property name="username" value="${name}"></property>
<property name="password" value="${pwd}"></property>
</bean> --------------------编程问答-------------------- <context:property-placeholder location="classpath:jdbc.properties/>
补充:Java , Java EE