Java Ninja Chronicles By Norris Shelton

Things I learned in the pursuit of code

Accessing properties via Spring is very easy. To read a properties file is very easy. This reads the given properties file from the file system and places the values in a map named rabbitConfiguration. It takes advantage of SpEl to get a system property and use that to resolve the file name.

<util:properties id="rabbitConfiguration" location="file:#{systemProperties.APP_CONFIG}/funding.properties"/>

My problems started when I tried to use the values from the properties file that contained periods. I tried to use them like:

    <bean id="rabbitConnectionFactory" class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory"
          c:hostname="#{rabbitConfiguration.vipre.host}"
          c:port="#{rabbitConfiguration.vipre.port}"
          p:username="#{rabbitConfiguration.vipre.user}"
          p:password="#{rabbitConfiguration.vipre.password}"/>

Spring did not like it. It thought that vipre was my property and host was something to be accessed off of it like an object. This is the exception that was thrown.

Caused by: org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 20): Property or field 'vipre' cannot be found on object of type 'java.util.Properties' - maybe not public?
	at org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:164)
	at org.springframework.beans.factory.support.AbstractBeanFactory.evaluateBeanDefinitionString(AbstractBeanFactory.java:1365)
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.doEvaluate(BeanDefinitionValueResolver.java:255)
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.evaluate(BeanDefinitionValueResolver.java:228)
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:204)
	at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:648)
	at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:140)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1137)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1040)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351)
	... 109 common frames omitted
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 20): Property or field 'vipre' cannot be found on object of type 'java.util.Properties' - maybe not public?
	at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:224)
	at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:94)
	at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:81)
	at org.springframework.expression.spel.ast.CompoundExpression.getValueRef(CompoundExpression.java:57)
	at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:87)
	at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:120)
	at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:242)
	at org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:161)
	... 124 common frames omitted

I searched and searched around the internet trying to find a way to work around. I tried single-quotes, back-slashes, double-quotes, double-quotes and single-quotes together. Nothing seemed to work, then it hit me. To get around this, I approached the properties access via the array/bracket syntax (#{rabbitConfiguration[‘vipre.port’]}

    <bean id="rabbitConnectionFactory" class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory"
          c:hostname="#{rabbitConfiguration['vipre.host']}"
          c:port="#{rabbitConfiguration['vipre.port']}"
          p:username="#{rabbitConfiguration['vipre.user']}"
          p:password="#{rabbitConfiguration['vipre.password']}"/>

May 31st, 2016

Posted In: Java, java ninja, Javaninja, SpEl, Spring

Leave a Reply

Your email address will not be published. Required fields are marked *

LinkedIn Auto Publish Powered By : XYZScripts.com