title: spring bean creation for java builder pattern objects
body: <p>I'm using Google Gson(gson) library form reading/writing json files and spring mvc 3 in my web application side.</p>
  
  <p>So In controller, I want to create a singleton instance of Gson with pretty printing. In java the code would be,</p>
  
  <pre><code>Gson gson = new GsonBuilder().setPrettyPrinting().create();
  </code></pre>
  
  <p>In Controller, I created an autowired entry as below,</p>
  
  <pre><code>@Autowired
  private Gson gson;
  </code></pre>
  
  <p>and the xml bean configuration is as below,</p>
  
  <pre><code>&lt;bean id="gsonBuilder" class="com.google.gson.GsonBuilder"&gt;
           &lt;property name="prettyPrinting" value="true"/&gt;
   &lt;/bean&gt;  
   &lt;bean id="gson" factory-bean="gsonBuilder" factory-method="create"/&gt;
  </code></pre>
  
  <p>It throws the following exception in catalina logs,</p>
  
  <pre><code>Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'prettyPrinting' of bean class [com.google.gson.GsonBuilder]: Bean property 'prettyPrinting' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
      at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:1024)
      at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:900)
      at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:76)
      at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:58)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1358)
  </code></pre>
  
  <p>I knew that the setter signature of setPrettyPrinting() is different than spring expects, that's why spring is throwing the exception.</p>
  
  <pre><code>  public GsonBuilder setPrettyPrinting() {
      prettyPrinting = true;
      return this;
    }
  </code></pre>
  
  <p>But I'm unable to find a way to wire the builder pattern beans. I'm pretty new to spring. Can any one let me know, whether it is possible to solve this problem in xml bean approach ?</p>