Announcement Announcement Module
Collapse
No announcement yet.
Populate properties on non-FactoryBean build objects Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Populate properties on non-FactoryBean build objects

    I have an object that is NOT instantiated by a BeanFactory, but that I would like to use Spring's @Value and @Autowire annotations. Specifically, I am trying to take a property and inject it into this object using @Value.

    I have my.properties with:
    country.city.zone = some_value

    I am then loading this property file using the standard PropertyPlaceholderConfigurer so that other objects that are instantiated by the BeanFactory can be populated through injection as normal.

    Target class looks something like this:
    Code:
    public class MyClass{
      @Value("#{systemProperties.country.city.zone}")
      private myTargetProperty;
    }
    Then I have a method that goes something like this. Object in this case would be an instance of MyClass.
    Code:
    public void injectSpringFactoryAnnotationValues(Object o){
      ConfigurableListableBeanFactory factory = /* Magically obtain */;
      factory.autowireBean(o);
    }
    When I run the method, I keep getting this exception:
    Code:
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'namespace.MyClass': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String namespace.MyClass.myTargetProperty; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 17): Field or property 'country' cannot be found on object of type 'java.util.Properties'
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1074)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBean(AbstractAutowireCapableBeanFactory.java:295)
    ...
    Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String namespace.MyClass.myTargetProperty; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 17): Field or property 'collection' cannot be found on object of type 'java.util.Properties'
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:502)
    	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:282)
    	... 49 more
    Caused by: org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 17): Field or property 'collection' cannot be found on object of type 'java.util.Properties'
    	at org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:141)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.evaluateBeanDefinitionString(AbstractBeanFactory.java:1287)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:715)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:703)
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:474)
    	... 51 more
    Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 17): Field or property 'country' cannot be found on object of type 'java.util.Properties'
    	at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:207)
    	at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:71)
    	at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:57)
    	at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:93)
    	at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:88)
    	at org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:138)
    	... 55 more

  • #2
    I figured it out, I almost had it.

    My problem was actually in the @Value expression. I should have used a $ instead of a #.

    That aside, works great.

    Comment

    Working...
    X