从另一个类的静态方法初始化Spring bean,给出方法参数?

时间:2013-08-27 14:56:35

标签: java spring static applicationcontext

如何使用需要参数的工厂方法初始化bean? 我找不到一个带参数的方法的例子,只有没有params方法... spring docs

由于

2 个答案:

答案 0 :(得分:2)

小心向下滚动你给出的一些文档?

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

答案 1 :(得分:0)

这就是你要找的东西:

<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;
  }
}

参考1

相关问题