mardi 12 mai 2015

Loading spring context could not resolve placeholder

I am trying to configure my application to load the properties from a file and substitute them into my spring context. i am doing this as part of an integration test. My test class has the following annotations

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:spring/test-database-service.xml", "classpath:/spring/test-data-loader.xml" })

I have the following in my application context test-data-loader.xml

<context:annotation-config />       
    <context:property-placeholder ignore-unresolvable="true" ignore-resource-not-found="true" location="classpath*:config/dataloader-test.properties" />
    <context:property-placeholder ignore-unresolvable="true" ignore-resource-not-found="true" location="file:/data/dataloader-test.properties" />

    <bean id="customResourceLoader" class="test.data.loader.excel.CustomResourceLoader">
        <property name="resource">
            <value>file:${dataloader.file.location}/${dataloader.filename}</value>
        </property>
    </bean>

The file dataloader-test.properties exists in the classpath in one of my jar files and when I put a debug point in the setlocations method of PropertySourcesPlaceholderConfigurer I can see that it is finding the correct jar file within my maven repository.

[URL [jar:file:/C:/Users/xx/.m2/repository/test-utils/2.0.0.3/test-utils-2.0.0.3.jar!/config/dataloader-test.properties]]  -- this exists and has the properties file inside it

So the exception I get when trying to load the application context for the test is

java.lang.IllegalStateException: Failed to load ApplicationContext
        at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:157)
        at test.data.loader.DataSetLoaderExecutionListener.getDataSource(DataSetLoaderExecutionListener.ja
va:433)
        at test.data.loader.DataSetLoaderExecutionListener.beforeTestClass(DataSetLoaderExecutionListener.
java:76)
        at org.springframework.test.context.TestContextManager.beforeTestClass(TestContextManager.java:288)
        at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCal
lbacks.java:60)
        at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallb
acks.java:71)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
        at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:283)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:173)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153)
        at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:128)
        at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:203)
        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:155)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'customReso
urceLoader' defined in class path resource [spring/test-data-loader.xml]: Could not resolve placeholder 'dataloader.file
.location' in string value [file:${dataloader.file.location}/${dataloader.filename}]
        at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigur
erSupport.java:209)
        at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.processProperties(PropertySourcesPla
ceholderConfigurer.java:174)
        at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.postProcessBeanFactory(PropertySourc
esPlaceholderConfigurer.java:151)
        at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplic
ationContext.java:686)
        at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplic
ationContext.java:661)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:451)
        at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoade
r.java:103)
        at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoade
r.java:1)
        at org.springframework.test.context.support.DelegatingSmartContextLoader.loadContext(DelegatingSmartContextLoade
r.java:228)
        at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:124)
        at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:148)
        ... 14 more

i am not sure what I am missing as I can see the properties file in the jar. and it has the values that I have referenced inside of it.

dataloader.file.location=target/resources/data
dataloader.filename=Data for Development.xlsx

thanks in advance

Aucun commentaire:

Enregistrer un commentaire