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 Comment

A common problem is to retrieve a property from a property files and to use that value within your code. The Springframework has several tools that make that very easy to do.

The first thing to do is to create your properties file. Mine contains the following:

aggregatorId=gsi
igpUrl=http://localhost:8080/igp-services
defaultSupplierId=voi

Next, you need to make Spring aware of the properties file. To do this, we will use the util schema. Line 4 shows that the properties has an id value. This allows us to later use that idid.key, instead of key.

xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
...
<util:properties id="gsiProperties" location="gaming-adapter-default.properties"/>

Now that you have the properties available to Spring, it’s time to use them. Notice that we are using Spring Expression Language (SpEl) to inject the value(…) of the key. Normally, I would use @Autowired or @Resource(name=”…”) to inject the properties object as a map, then call get(…) on the map. SpEl gives you an additional notational choice to inject the data. @value(…) can be placed on fields, methods, method parameters and constructor parameters. Inside the @value, you can use #{…}. You do not need # for predefined variables (e.g. @value({systemProperties.property})). If the property cannot be listed directly because it contains a period, etc, you can place it within square brackets with a single tick. An example is @Value(#{systemProperties[‘user.region’]}).

    @Value("#{gsiProperties.igpUrl}")
    private String igpUrl;

    @Value("#{gsiProperties.aggregatorId}")
    private String aggregatorId;

    @Value("#{gsiProperties.defaultSupplierId}")
    private String defaultSupplierId;
...
    model.setIdAggregator(aggregatorId);
    model.setIdGameSupplier(defaultSupplierId);

July 23rd, 2015

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

Tags: , , , ,

Leave a Comment

I have a Springframework project that had a property value that contained the host of the server that I was going to be deployed on. There was more to it, because there were profiles, etc, but that is the gist of it. There is a value in a property file that I need.

The properties file was declared as:

<util:properties id="commonProperties" location="classpath:common-default.properties"/>

The contents of the properties file contained:

hostUrl=localhost:8080

To use the value in the jsp, pull in the Spring tags by:

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>

Tie it all together by

<spring:eval var="loginUrl" expression="@commonProperties.hostUrl"/>
<a href="http://${loginUrl}/hub/login.jsp">Return to login</a>

January 27th, 2014

Posted In: JSP, SpEl, Spring

One Comment

LinkedIn Auto Publish Powered By : XYZScripts.com