Announcement Announcement Module
Collapse

JavaConfig forum decommissioned in favor of Core Container

As described at

http://static.springsource.org/sprin...fig/README.TXT

key features of the Spring JavaConfig project have been migrated into the core Spring Framework as of version 3.0.

Please see the Spring 3.0 documentation on @Configuration and @Bean support:

http://static.springsource.org/sprin...tml#beans-java

For any questions related to @Configuration classes and @Bean methods in Spring 3.0, please post in the dedicated 'Core Container' forum at

http://forum.springsource.org/forumdisplay.php?f=26
See more
See less
How to read/load a properties file for @Value ? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to read/load a properties file for @Value ?

    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

  • #2
    I switched from PropertyPlaceholderConfigurer to PropertiesFactoryBean (see below), and now @Value is working as long as I'm not using default values:
    * @Value("#{url}") works fine
    * @Value("#{url}:myDefault") throws SpelEvaluationException when url is not set in the properties file (see stacktrace below)


    Config class using PropertiesFactoryBean:

    @Configuration
    public class SpringProperties implements BeanFactoryAware, ApplicationContextAware {
    private AutowireCapableBeanFactory autowireCapableBeanFactory;
    private ApplicationContext applicationContext;

    @Bean
    public Properties springProps() {
    Resource propertiesFile = new ClassPathResource("META-INF/spring.properties");
    PropertiesFactoryBean factoryBean = new PropertiesFactoryBean();
    factoryBean.setLocation(propertiesFile);

    assert autowireCapableBeanFactory != null;
    PropertiesFactoryBean configuredFactoryBean = (PropertiesFactoryBean) autowireCapableBeanFactory.initializeBean(factoryB ean, null);

    if (applicationContext != null) {
    if (configuredFactoryBean instanceof ResourceLoaderAware) {
    ((ResourceLoaderAware) configuredFactoryBean).setResourceLoader(applicati onContext);
    }
    if (configuredFactoryBean instanceof ApplicationContextAware) {
    ((ApplicationContextAware) configuredFactoryBean).setApplicationContext(appli cationContext);
    }
    }

    Properties properties = null;
    try {
    properties = configuredFactoryBean.getObject();
    }
    catch (IOException e) {
    throw new IllegalStateException(e);
    }

    return properties;
    }

    @Override // Interface BeanFactoryAware
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
    if (beanFactory instanceof AutowireCapableBeanFactory) {
    autowireCapableBeanFactory = (AutowireCapableBeanFactory) beanFactory;
    }
    }


    @Override // Interface ApplicationContextAware
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    this.applicationContext = applicationContext;
    }
    }


    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 12): Field or property 'url' cannot be found on object of type 'java.util.Properties'
    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 12): Field or property 'url' cannot be found on object of type 'java.util.Properties'
    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 12): Field or property 'url' cannot be found on object of type 'java.util.Properties'
    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 12): Field or property 'url' cannot be found on object of type 'java.util.Properties'
    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.CompoundEx pression.getValueInternal(CompoundExpression.java: 57)
    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

    Comment


    • #3
      Hi Juergen,

      I haven't looked closely at your posts herein, but want to quickly point out that there are open issues regarding using @Value within @Configuration classes. Take a look if you haven't already seen them:

      http://jira.springframework.org/browse/SPR-6611
      http://jira.springframework.org/browse/SPR-6613

      If above is still a problem for you, please consider adding a JIRA issue with an executable example that demonstrates the problem. A simple JUnit test would do. If there are any dependencies on other frameworks in order to exhibit the behavior, a Maven POM is preferred.

      Also, as a friendly reminder, consider using [ code ] [/ code ] tags when pasting in code, stack traces, etc here on the forum. It improves readability quite a bit.

      Thanks!

      Comment


      • #4
        I filed the JIRA issue http://jira.springframework.org/browse/SPR-6806 and provided a testcase.

        Comment

        Working...
        X