Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using a .properties file with a Custom Bean Parser

I have a custom implementation of an AbstractSingleBeanDefinitionParser to allow me to define 3D Vectors in my spring config with less... ceremony than would otherwise be required.

<rbf:vector3d id="test_vector" delimeter=";" value="45;46;47"/>

That works great, and I have been using it for months without any problems. Yesterday I tried to define the value in a .properties file like this:

In test.properties I have:

vector3d.value=1,2,3

And in the xml file I have:

<context:property-placeholder location="test.properties"/>
<rbf:vector3d id="test_vector_with_properties" delimeter="," value="${vector3d.value}"/>

When I try to run my unit test, it crashes, and I get this exception:

Caused by: java.lang.NumberFormatException: For input string: "${vector3d.value}"
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1222)
    at java.lang.Double.parseDouble(Double.java:510)
    at scala.collection.immutable.StringLike$class.toDouble(StringLike.scala:234)
    at scala.collection.immutable.StringOps.toDouble(StringOps.scala:31)
    at rb.foundation.spring.xml.Vector3DBeanDefinitionParser$$anonfun$1.apply(Vector3DBeanDefinitionParser.scala:25)

When I use the .properties file for normal beans, it works great, which leads me to believe that there is a subtlety that I overlooked in my implemention of my parser. It's written in scala, but you should be able to follow it:

class Vector3DBeanDefinitionParser extends AbstractSingleBeanDefinitionParser
{
  override def getBeanClass(element : Element) = classOf[Vector3D]

  override def doParse(element: Element, builder: BeanDefinitionBuilder)
  {
    val delim = element.getAttribute("delimeter")
    val value = element.getAttribute("value")

    val values = value.split(delim).map(_.toDouble)

    builder.addConstructorArgValue(values(0))
    builder.addConstructorArgValue(values(1))
    builder.addConstructorArgValue(values(2))
  }
}

I'm happy to add the key substitution if necessary, I just need to know where/how to do it.

Ideas?

like image 314
fbl Avatar asked Oct 17 '12 14:10

fbl


People also ask

How do you call a properties file in spring boot?

Another method to access values defined in Spring Boot is by autowiring the Environment object and calling the getProperty() method to access the value of a property file.


1 Answers

So the reason this doesn't work is that your BeanDefinitionParser runs much before property placeholders are resolved. Simple overview as I understand it:

  1. BeanDefinitionParsers parse the XML into BeanDefinition objects in memory
  2. BeanDefinitions are then loaded into a BeanFactory
  3. BeanFactoryPostProcessors (including the property placeholder configurers) are executed on the bean definitions
  4. beans are created from the bean definitions

(Of course other things happen along the way, but those are the relevant steps here.)

So in order to get the resolved property value into your Vector3D object, I think you're going to have to delay specifying the arguments to the Vector3D constructor until after BeanFactoryPostProcessors have run. One way that occurs to me is to have your BeanDefinitionParser construct a bean definition for a Spring FactoryBean instead of the Vector3D itself. Then the splitting of the vector value that you currently have in your Vector3DBeanDefinitionParser would need to be in the FactoryBean implementation instead.

Sorry, I'm not too familiar with Scala so this will be in Java.

The FactoryBean class would look something like this:

import org.springframework.beans.factory.FactoryBean;

public class Vector3DFactoryBean implements FactoryBean<Vector3D> {
    private String delimiter;
    private String value;
    private transient Vector3D instance;

    public String getDelimiter() { return delimiter; }
    public void setDelimiter(String delimiter) { this.delimiter = delimiter; }
    public String getValue() { return value; }
    public void setValue(String value) { this.value = value; }

    @Override
    public Vector3D getObject() {
        if (instance == null) {
            String[] values = value.split(delimiter);
            instance = new Vector3D(
                                    Double.parseDouble(values[0]),
                                    Double.parseDouble(values[1]),
                                    Double.parseDouble(values[2])
                                   );
        }
        return instance;
    }
    @Override
    public Class<?> getObjectType() {
        return Vector3D.class;
    }
    @Override
    public boolean isSingleton() {
        return true;
    }
}

Then your Vector3DBeanDefinitionParser would just pass the delimiter and value untouched to the Vector3DFactoryBean bean definition:

class Vector3DBeanDefinitionParser extends AbstractSingleBeanDefinitionParser
{
  override def getBeanClass(element : Element) = classOf[Vector3DFactoryBean]

  override def doParse(element: Element, builder: BeanDefinitionBuilder)
  {
    val delim = element.getAttribute("delimeter")
    val value = element.getAttribute("value")

    builder.addPropertyValue("delimiter", delim)
    builder.addPropertyValue("value", value)
  }
}

Then later when the placeholder property configurer runs, it should resolve the property values in the Vector3DFactoryBean bean definition. When beans are finally created from bean definitions, the Vector3DFactoryBean will parse the vector values and create the Vector3D object.

like image 94
matts Avatar answered Oct 12 '22 07:10

matts