spring中bean标签factory-method和factory-bean)详解工厂方法(factory-method和factory-bean)

转自:http://blog.sina.com.cn/s/blog_6d3c1ec601019f3j.html

A、factory-method

The name of a factory method to use to create this object.

工厂方法名称用于创建这个对象。

Use constructor-arg elements to specify arguments to the factory method, if it takes arguments.

若这个工厂方法需要参数的话,使用constructor-arg 元素来指定它的参数。

Autowiring does not apply to factory methods.

自动绑定不能用于工厂方法。

If the "class" attribute is present, the factory method will be a static method on the class specified by the "class" attribute on this bean definition.

若class属性存在,那么这个工厂方法将是这个类内部的一个指向这个类的静态方法。

Often this will be the same class as that of the constructed object - for example, when the factory method is used as an alternative to a constructor.

通常它和构造对象是相同的类,例如,它可代替构造函数。

However, it may be on a different class. In that case, the created object will *not* be of the class specified in the "class" attribute.

它也可以是不同的类。在这种情况下,被创建的对象将不是class属性所指定的类了。

This is analogous to FactoryBean behavior. If the "factory-bean" attribute is present, the "class" attribute is not used, and the factory method will be an instance method on the object returned from a getBean call with the specified bean name.

这和FactoryBean行为相似。若factory-bean属性存在,那么class属性将不会被使用,这个工厂方法将会是通过指定bean名称调用getBean返回对象的一个实例方法。

The factory bean may be defined as a singleton or a prototype. The factory method can have any number of arguments. Autowiring is not supported. Use indexed constructor-arg elements in conjunction with the factory-method attribute. Setter Injection can be used in conjunction with a factory method. Method Injection cannot, as the factory method returns an instance, which will be used when the container creates the bean.

这个工厂bean可以定义为singleton或prototype。这个工厂方法有任何数量参数。不支持自动绑定。使用索引constructor-arg属性结合factory-method属性。Setter注入可以和工厂方法结合使用。方法注入不可以使用,作为工厂方法返回的实例,它将使用容器创建的bean。

举例说明

范例1

1. ExampleBean4

public class ExampleBean4 {

   private String abc="123";

   public String getAbc() {

      return abc;

    }

   public void setAbc(String abc) {

      this.abc = abc;

    }

   public static ExampleBean4 createInstance(){

       ExampleBean4 exampleBean4=new ExampleBean4();

       exampleBean4.setAbc("456");

      return exampleBean4;

    }

}

2.配置文件

<beans>

    <bean id="bean1" class="IoC.ExampleBean4" />

<bean id="bean2" class="IoC.ExampleBean4" factory-method="createInstance"/>

</beans>

3.测试类

public class Test {

   public static void main(String[] args) {

       String fileName = "bean5.xml";

       AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(

             new String[] { fileName });

       ExampleBean4 bean1=(ExampleBean4)ctx.getBean("bean1");

       System.out.println(bean1.getAbc());

       ExampleBean4 bean2=(ExampleBean4)ctx.getBean("bean2");

       System.out.println(bean2.getAbc());

    }

}

4.运行结果

123

456

Bean1是使用构造函数创建的,故它返回123.bean2是使用工厂方法创建的,故返回456.

范例2

若我们把ExampleBean4改为如下,工厂方法的返回值和它本身的类不一样,这时在调用getBean()将返回String对象,而不是ExampleBean4

public class ExampleBean4 {

   private String abc="123";

   public String getAbc() {

      return abc;

    }

   public void setAbc(String abc) {

      this.abc = abc;

    }

   public static String createInstance(){

      return "789";

    }

}

 

B、factory-bean

Alternative to class attribute for factory-method usage.

在使用factory-method时,替代class属性。

If this is specified, no class attribute should be used. This must be set to the name of a bean in the current or ancestor factories that contains the relevant factory method. This allows the factory itself to be configured using Dependency Injection, and an instance (rather than static) method to be used.

若指定了这个属性,class属性将不会被使用。它必须设定为当前或父工厂中的bean名称,它包含相关的工厂方法。这使得工厂本身需要配置依赖注入,并使用一个实例方法(不是静态的)。

举例说明

我们根据上面的例子,修改为使用factory-bean来创建bean。这时候createInstance工厂方法不再必须是静态的。

范例5

1. ExampleBean5

public class ExampleBean5 {

   private String abc="123";

   public String getAbc() {

      return abc;

    }

   public void setAbc(String abc) {

      this.abc = abc;

    }

   public ExampleBean5 createInstance(){

       ExampleBean5 exampleBean5=new ExampleBean5();

       exampleBean5.setAbc("456");

      return exampleBean5;

    }

}

2.配置文件

<beans>

    <bean id="bean1" class="IoC.ExampleBean5" />

<bean id="bean2" factory-method="createInstance" factory-bean="bean1"/>

</beans>

3.测试类

不变

4.运行结果

不变

原文地址:https://www.cnblogs.com/sharpest/p/7784097.html