1.在配置applicationContext-dao.xml中,如果加载资源文件 jdbc.properties 和 dataSource 配置 不在同一个文件时,注意对jdbc.properties的扫描路径采用classpath*:
例如:将spring-dao.xml 拆分为: applicationContext-source.xml 和 applicationContext-dao.xml
applictionContext-source.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 加载所有资源文件 -->
<context:property-placeholder
location="classpath*:*.properties" />
</beans>
applictionContext-dao.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-4.0.xsd">
<bean id="dataSource"
class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc_driver}"/>
<property name="url" value="${jdbc_url}"/>
<property name="username" value="${jdbc_username}"/>
<property name="password" value="${jdbc_password}"/>
<property name="maxActive" value="10"/>
<property name="minIdle" value="5"/>
</bean>
<!-- 配置SqlSessionFactory对象 -->
<bean name="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据库连接池 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 加载mybatis的全局配置文件 -->
<property name="configLocation"
value="classpath:SqlMapConfig.xml"></property>
<!-- 扫描model包 使用别名 -->
<property name="typeAliasesPackage" value="cn.wz.ssm.bean"/>
<!-- 扫描sql配置文件:mapper需要的xml文件 -->
<property name="mapperLocations" value="classpath*:cn/wz/ssm/mapper/*.xml"/>
</bean>
</beans>
否则在测试时报错:
java.lang.IllegalStateException: Failed to load ApplicationContext
... more
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'dataSource' defined in file [G:\ADataIDEA\TestMaven\target\classes\applicationContext-dao.xml]: Could not resolve placeholder 'jdbc_driver' in value "${jdbc_driver}"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'jdbc_driver' in value "${jdbc_driver}"
... more
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'jdbc_driver' in value "${jdbc_driver}"
... more
2.1.在配置applicationContext-dao.xml中,如果加载资源文件 jdbc.properties 和 dataSource 配置 不在同一个文件时,注意不可多次扫描 jdbc.properties 配置文件
Could not resolve placeholder 'jdbc.driver' in string value "${jdbc.driver}"