title: Creating beans with BeanFactoryPostProcessor
body: <h2>Spring BeanFactoryPostProcessor problem</h2>
  
  <p>I want to create a Spring BeanFactoryPostProcessor that add beans to the current ApplicationContext.</p>
  
  <p>I have a lot of Web-Services definition in my <code>spring-ws-config.xml</code> and I want to reduce as much as possible.</p>
  
  <h3>XML Configuration</h3>
  
  <p>The configuration looks like:</p>
  
  <pre><code>&lt;bean id="menu"
      class="org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition"
      lazy-init="true"&gt;
      &lt;property name="schemaCollection"&gt;
          &lt;bean
              class="org.springframework.xml.xsd.commons.CommonsXsdSchemaCollection"&gt;
              &lt;property name="inline" value="true" /&gt;
              &lt;property name="xsds"&gt;
                  &lt;list&gt;
                      &lt;value&gt;classpath:xsd.xsd&lt;/value&gt;
                  &lt;/list&gt;
              &lt;/property&gt;
          &lt;/bean&gt;
      &lt;/property&gt;
      &lt;property name="portTypeName" value="portType" /&gt;
      &lt;property name="serviceName" value="serviceName" /&gt;
      &lt;property name="locationUri" value="/endpoints" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <h3>Java Configuration</h3>
  
  <p>So, I create a @Configuration class with the following bean definition:</p>
  
  <pre><code>@Bean
  @Lazy
  public DefaultWsdl11Definition webService() throws IOException {
  
      logger.info("Creating Web Service");
      DefaultWsdl11Definition toRet = new DefaultWsdl11Definition();
      toRet.setPortTypeName("portType");
      toRet.setServiceName("serviceName");
  
      CommonsXsdSchemaCollection collection = new CommonsXsdSchemaCollection();
      collection.setInline(true);
      collection.setXsds(new Resource[] { new ClassPathResource("path1") });
      collection.afterPropertiesSet();
  
      toRet.setSchemaCollection(collection);
      toRet.setLocationUri("/endpoints");
      return toRet;
  
  }
  </code></pre>
  
  <p>This is much better!, but I want to reduce it more, so I want to create a annotation called @WebServiceDefinition, and add a BeanFactoryPostProcessor to create the beans automatically, so I wrote this:</p>
  
  <h3><a href="https://gist.github.com/aVolpe/7047456" rel="nofollow">BeanFactoryPostProcessor</a></h3>
  
  <pre><code>@Override
  public void postProcessBeanFactory(ConfigurableListableBeanFactory bf)
          throws BeansException {
  
      Map&lt;String, Object&gt; beans = bf.getBeansWithAnnotation(WebService.class);
  
      for (Entry&lt;String, Object&gt; entry : beans.entrySet()) {
          Object bean = entry.getValue();
          WebService ws = bean.getClass().getAnnotation(WebService.class);
          String name = getName(entry.getKey());
          DefaultWsdl11Definition newWS = createWebService(name, ws.xsds());
  
          bf.registerSingleton(name, newWS);
      }
  }
  </code></pre>
  
  <p><strong>But, this doesn't works!, I wrote a simple test, you can see it <a href="https://gist.github.com/aVolpe/7047488" rel="nofollow">here</a></strong></p>
  
  <p>I see that the IOC don't work with the classes with the annotations, this is because the method: BeanFactory#getBeansWithAnnotation don't initialize it, mark it as created, and dont inject anything.</p>
  
  <h2>Workaround</h2>
  
  <p>I do a workaround: get all beans by name, get the corresponde class and use #bf.getBeansOfType(Class), (this method dont initialize it!).</p>
  
  <h2>My questions:</h2>
  
  <ul>
  <li>This is a valid workaround?</li>
  <li>How I can use the method #getBeansWithAnnotation() and don't initialize the bean?</li>
  </ul>