Announcement Announcement Module
Collapse
No announcement yet.
[Possible Bug] PropertyPlaceholderConfigurer doesn't find resource with * Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • [Possible Bug] PropertyPlaceholderConfigurer doesn't find resource with *

    Dear all,

    I have created the attached project to illustrate what I wonder is a bug.

    I am using the PropertyPlaceholderConfigurer in the usual fashion, with a wildcard:

    Code:
    	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">		
    		<property name="locations">
    			<list>
                                    <value>classpath:/whateve*.properties
    				</value>
    			</list>
    		</property>
    	</bean>
    However, even though I have a file named whatever.properties (notice the r instead of *) in dependencyA, Spring is not finding the file in myProject. If, however, I replace the * with an r, the file is found.

    This is shown in the attached project with the layout:
    ParentA:
    - Module1
    - Module2

    Module2's application context <import resource="classpath:/originalApplicationContext.xml"/> - which is in Module1.

    1) I created a SpringPropertiesUtil extension of PropertyPlaceholderConfigurer to illustrate the issue
    2) Notice Module1's OriginalSpringPropertiesTest: because we have myproperties.properties in Module1, Spring finds "myProp" and the test passes
    3) Module2's CopiedSpringPropertiesTest: the test is the same, but it fails: it doesn't find "myProp"
    4) If you switch the commented lines in originalApplication context (ie, add the classpath:myproperties.properties and forget about wildcards) CopiedSpringPropertiesTest of step 3 passes

    Could you confirm this and
    a) confirm this is bug
    b) if not, why it doesn't work with wildcards

  • #2
    Move parentA\module1\src\main\resources files into parentA\module1\src\test\resources, then test success.

    and module2 test failed owing to not found originalApplicationContext.xml in test classpath.

    Comment


    • #3
      Not really Kent:

      1) Moving originalApplicationContext.xml and myproperties.properties in Module1 from main/resources to test/resources makes (module2's) CopiedSpringPropertiesTest yield the error [1]

      2) With the original code, (module2's) CopiedSpringPropertiesTest fails with a NullPointer at propertiesMap.get(name).toString(); - because it didn't find (module1's) myproperties.properties and therefore propertiesMap is null.

      3) If you leave the resources where they are and change the PropertiesPlaceholderConfigurer bean in originalApplicationContext.xml to <value>classpath:myproperties.properties</value>, the module2/CopiedSpringPropertiesTest is green

      So the issue is not where you pointed.


      [1]
      Code:
      SEVERE: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@2c79809] to prepare test instance [spring.CopiedSpringPropertiesTest@7dce784b]
      java.lang.IllegalStateException: Failed to load ApplicationContext
      	at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:308)
      	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
      	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
      	at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
      	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
      	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)
      	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
      	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)
      	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
      	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48)
      	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
      	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
      	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
      	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
      	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
      	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
      	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
      	at org.junit.runners.ParentRunner.run(ParentRunner.java:292)
      	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
      	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
      	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
      	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
      	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
      	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
      	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
      Caused by: org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from URL location [classpath:/originalApplicationContext.xml]
      Offending resource: class path resource [applicationContext.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [originalApplicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [originalApplicationContext.xml] cannot be opened because it does not exist
      	at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)
      	at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)
      	at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:76)
      	at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.importBeanDefinitionResource(DefaultBeanDefinitionDocumentReader.java:192)
      	at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseDefaultElement(DefaultBeanDefinitionDocumentReader.java:147)
      	at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:132)
      	at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:93)
      	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:493)
      	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:390)
      	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
      	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
      	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
      	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
      	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)
      	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:212)
      	at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:81)
      	at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:1)
      	at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:280)
      	at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:304)
      	... 24 more
      Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [originalApplicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [originalApplicationContext.xml] cannot be opened because it does not exist
      	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
      	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
      	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
      	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
      	at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.importBeanDefinitionResource(DefaultBeanDefinitionDocumentReader.java:186)
      	... 39 more
      Caused by: java.io.FileNotFoundException: class path resource [originalApplicationContext.xml] cannot be opened because it does not exist
      	at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:158)
      	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
      	... 43 more

      Comment


      • #4
        For those interested: this is due to the way we have to lookup resources via the classloader. From http://static.springsource.org/sprin...terns-in-paths :

        If the specified path is a classpath location, then the resolver must obtain the last non-wildcard path segment URL via a Classloader.getResource() call. Since this is just a node of the path (not the file at the end) it is actually undefined (in the ClassLoader Javadocs) exactly what sort of a URL is returned in this case. In practice, it is always a java.io.File representing the directory, where the classpath resource resolves to a filesystem location, or a jar URL of some sort, where the classpath resource resolves to a jar location. Still, there is a portability concern on this operation.
        So we should have the properties inside a folder for the resource lookup to work as expected:
        Code:
        <value>classpath:myLocation/mypropertie*.properties</value>
        In this case, the resources are loaded as expected.
        Last edited by wild_oscar; Nov 8th, 2011, 04:40 AM. Reason: layout change

        Comment

        Working...
        X