Question

How can you initialize a bean by using a factory method that needs a parameter? I cannot find an example with a method that has parameter, only no params methods... spring docs

Thanks

Was it helpful?

Solution

Care to scroll down a bit of the docs you gave?

<bean id="exampleBean" class="examples.ExampleBean"
      factory-method="createInstance">
  <constructor-arg ref="anotherExampleBean"/>
  <constructor-arg ref="yetAnotherBean"/>
  <constructor-arg value="1"/> 
</bean>

OTHER TIPS

Is this what you are looking for :

<bean id="clientService"
      class="examples.ClientService"
      factory-method="createInstance"/>

public class ClientService {
  private static ClientService clientService = new ClientService();
  private ClientService() {}

  public static ClientService createInstance() {
    return clientService;
  }
}

Reference 1

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top