报错内容:Failed to load ApplicationContext

Invalid bean definition with name 'dataSource' defined in URL [file:/D:/KaiFa/pals/IBC/target/classes/spring-mybatis.xml]: Could not resolve placeholder 'jdbc.driver' in string value "${jdbc.driver}"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'jdbc.driver' in string value "${jdbc.driver}"

Could not resolve placeholder 'jdbc.driver' in string value "${jdbc.driver}"

贴一些关键错误,就不全贴了.错误说的在单元测试加载配置文件的时候无法加载jdbc.properties中的配置参数


最初以为是配置文件路径写错了,经过排查后没有问题

检查了引包  spring-test的版本是4.0.2.RELEASE

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>${spring.version}</version>
</dependency>

这是单元测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:/spring-mvc.xml","classpath*:/spring-mybatis.xml"})
public class BaseDao {
    @Resource
    private HomeXmlService homeXmlService;
    @Test
    public void csh() {
        HomeDao c = new HomeDao();
        homeXmlService.setXmlData(c);
    }
}



最后确认是因为xml配置问题,在单元测试加载了spring-mvc和spring-mybatis两个文件

在两个配置中我分别加载了Properties配置文件。

spring-mybatis.xml

<bean id="propertyConfigurer"
   class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
   <property name="location" value="classpath:jdbc.properties" />
</bean>

spring-mvc.xml

<bean class="com.ibc.util.CustomizedPlaceHolder">
   <property name="locations">
      <list>
         <value>classpath:prop/url.properties</value>
         <value>classpath:prop/synthetic-ratio.properties</value>
      </list>
   </property>
</bean>

经过不断排查和发现是因为分别引用了properties的问题

后因为是spring-mybatis报错,就在家在配置中加入

<property name="ignoreUnresolvablePlaceholders" value="true" />

调试后发现仍然有问题

最终确认,不止spring-mybaits,spring-mvc也要加这句,配置属性的字面意思就是忽略不肯舍弃的占位符

问题解决。