title: Initializing Spring bean from static method from another Class?
body: <p>I was trying to create Hibernate Validator bean, and run into this problem creating a bean from static factory method in another Class. I found a Spring way to get my Validator bean initialized (solution at the bottom), but the problem itself remains unsolved. Validator is used as example case here.</p>
  
  <p>This is how I create the Validator instance in Java</p>
  
  <pre><code>import javax.validation.Validation;
  import javax.validation.Validator;
  import javax.validation.ValidatorFactory;
  
  ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
  Validator validator = factory.getValidator();
  </code></pre>
  
  <p>This is how I tried to create the bean in applicationContext.xml</p>
  
  <pre><code>&lt;bean id="validatorFactory" 
      class="javax.validation.ValidatorFactory" 
      factory-method="javax.validation.Validation.buildDefaultValidatorFactory" /&gt;
  
  &lt;bean id="validator" 
      class="javax.validation.Validator" 
      factory-bean="validatorFactory"
      factory-method="getValidator" /&gt;
  </code></pre>
  
  <p>What I understand is that in "factory-method" you can only access static methods defined in the Class defined in the "class" parameter. Since the method buildDefaultValidatorFactory() is static I cant create a instance of Validation and give it as "factory-bean" for the validatorFactory like this:</p>
  
  <pre><code>&lt;bean id="validation" class="javax.validation.Validation" /&gt;
  
  &lt;bean id="validatorFactory" 
      class="javax.validation.ValidatorFactory" 
      factory-bean="validation"
      factory-method="buildDefaultValidatorFactory" /&gt;
  </code></pre>
  
  <p>This ends up to error message </p>
  
  <blockquote>
    <p>"Check that a method with the specified name exists and that it is <strong>non-static</strong>"</p>
  </blockquote>
  
  <p>Question is how would you create bean in this kind of a situation in Spring?</p>
  
  <p>This is how I solved the Validator problem:</p>
  
  <pre><code>&lt;bean id="validator"
    class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/&gt;
  </code></pre>