spring factory-method 动态传参数

spring factory-method 动态传参数(转)

转载自 http://forum.springsource.org/showthread.php?t=50934

   1: class ExampleBean {
   2:   private String string;
   3:   public ExampleBean(String string) {
   4:     this.string = string;
   5:   }
   6:   
   7:   public void write() {
   8:     System.out.println("The text is: " + text);
   9:   }
  10:  
  11: }
  12:  
  13: class ExampleBeanFactory {
  14:   public static ExampleBean createExampleBean(String string) {
  15:     return new ExampleBean(string);
  16:   }
  17: }
  18:  
  19: public class Main {
  20:   public static void main(String[] args) {
  21:     ApplicationContext context = new ClassPathXmlApplicationContext(
  22:                     "context.xml");
  23:     ExampleBean exampleBean = 
  24:             ExampleBean)context.getBean("exampleBean", 
  25:               new Object[]{"bla bla"});
  26:     exampleBean.write();
  27:   }
  28: }
   1: <bean id="exampleBean" class="...ExampleBeanFactory"  scope="prototype"
   2:         factory-method="createExampleBean">
   3:     <constructor-arg value="default value"/>
   4: </bean>

 

注意scope使用prototype

你可能感兴趣的:(spring factory-method 动态传参数)