When using JavaConfig as part of Spring 3.0.1 (nightly build) I want to read a properties file. So far I tried the following without success:
Properties file META-INF/spring.properties:
url = ....
Java config class to read the properties file:
@Configuration
public class SpringProperties {
@Bean
public PropertyPlaceholderConfigurer springProps() {
Resource propertiesFile = new ClassPathResource("META-INF/spring.properties");
PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer();
propertyPlaceholderConfigurer.setLocation(properti esFile);
return propertyPlaceholderConfigurer;
}
}
Java config class to use the properties as read above:
@Configuration
public class SpringConfig implements BeanFactoryAware, ApplicationContextAware {
@Value("#{url}")
private String url;
...
}
Now I get this stacktrace:
org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'dbUnit': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Could not autowire field: private java.lang.String de.hska.test.util.DbUnit.url; nested exception is org.springframework.beans.factory.BeanExpressionEx ception: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluation Exception: EL1008Epos 0): Field or property 'url' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpr essionContext'
at org.springframework.beans.factory.annotation.Autow iredAnnotationBeanPostProcessor.postProcessPropert yValues(AutowiredAnnotationBeanPostProcessor.java: 283)
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.populateBean(AbstractAu towireCapableBeanFactory.java:1055)
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:511)
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:450)
at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 90)
at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:222)
at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:287 )
at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:189)
at org.springframework.beans.factory.support.DefaultL istableBeanFactory.preInstantiateSingletons(Defaul tListableBeanFactory.java:561)
at org.springframework.context.support.AbstractApplic ationContext.finishBeanFactoryInitialization(Abstr actApplicationContext.java:842)
at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:416)
at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:139)
at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:83)
at de.hska.test.util.DbUnit.reload(DbUnit.java:84)
at de.hska.test.BestellverwaltungTest.reloadDB(Bestel lverwaltungTest.java:75)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:613)
at org.junit.runners.model.FrameworkMethod$1.runRefle ctiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallabl e.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExpl osively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.RunBefores.e valuate(RunBefores.java:27)
at org.springframework.test.context.junit4.statements .RunBeforeTestClassCallbacks.evaluate(RunBeforeTes tClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements .RunAfterTestClassCallbacks.evaluate(RunAfterTestC lassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.ja va:236)
at org.springframework.test.context.junit4.SpringJUni t4ClassRunner.run(SpringJUnit4ClassRunner.java:180 )
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestR eference.run(JUnit4TestReference.java:46)
at org.eclipse.jdt.internal.junit.runner.TestExecutio n.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.main(RemoteTestRunner.java:197)
Caused by: org.springframework.beans.factory.BeanCreationExce ption: Could not autowire field: private java.lang.String de.hska.test.util.DbUnit.url; nested exception is org.springframework.beans.factory.BeanExpressionEx ception: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluation Exception: EL1008Epos 0): Field or property 'url' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpr essionContext'
at org.springframework.beans.factory.annotation.Autow iredAnnotationBeanPostProcessor$AutowiredFieldElem ent.inject(AutowiredAnnotationBeanPostProcessor.ja va:504)
at org.springframework.beans.factory.annotation.Injec tionMetadata.inject(InjectionMetadata.java:84)
at org.springframework.beans.factory.annotation.Autow iredAnnotationBeanPostProcessor.postProcessPropert yValues(AutowiredAnnotationBeanPostProcessor.java: 280)
... 32 more
Caused by: org.springframework.beans.factory.BeanExpressionEx ception: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluation Exception: EL1008Epos 0): Field or property 'url' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpr essionContext'
at org.springframework.context.expression.StandardBea nExpressionResolver.evaluate(StandardBeanExpressio nResolver.java:140)
at org.springframework.beans.factory.support.Abstract BeanFactory.evaluateBeanDefinitionString(AbstractB eanFactory.java:1245)
at org.springframework.beans.factory.support.DefaultL istableBeanFactory.doResolveDependency(DefaultList ableBeanFactory.java:695)
at org.springframework.beans.factory.support.DefaultL istableBeanFactory.resolveDependency(DefaultListab leBeanFactory.java:684)
at org.springframework.beans.factory.annotation.Autow iredAnnotationBeanPostProcessor$AutowiredFieldElem ent.inject(AutowiredAnnotationBeanPostProcessor.ja va:475)
... 34 more
Caused by: org.springframework.expression.spel.SpelEvaluation Exception: EL1008Epos 0): Field or property 'url' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpr essionContext'
at org.springframework.expression.spel.ast.PropertyOr FieldReference.readProperty(PropertyOrFieldReferen ce.java:180)
at org.springframework.expression.spel.ast.PropertyOr FieldReference.getValueInternal(PropertyOrFieldRef erence.java:60)
at org.springframework.expression.spel.ast.SpelNodeIm pl.getValue(SpelNodeImpl.java:93)
at org.springframework.expression.spel.standard.SpelE xpression.getValue(SpelExpression.java:88)
at org.springframework.context.expression.StandardBea nExpressionResolver.evaluate(StandardBeanExpressio nResolver.java:137)
... 38 more


pos 0): Field or property 'url' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpr essionContext'
