转自: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.运行结果

不变

最新文章

  1. Linux ——————用Secure传文件时直接拖了文件用的是AssIC导致linux那边直乱码
  2. Lua 排行榜更新
  3. spring mvc 第二天【注解实现springmvc Handler处理ajax简单请求 的配置】
  4. 硬连接与软连接,inode与links
  5. 如何在真机上调试Android应用程序(图文详解)(zz)
  6. vmdk虚拟机转换为OVF模板,导入esxi
  7. dojo 九 effects dojo/_base/fx 和 dojo/fx
  8. 常规页生命周期(class0620)
  9. jquery easy ui 学习 (5) windowlayout
  10. keychain的使用
  11. openstack私有云布署实践【12.1 网络Neutron-controller节点配置(科兴环境)】
  12. Android 6.0的权限问题
  13. 使用Git上传项目代码到github
  14. linux RHCS集群 高可用web服务器
  15. Linux的发行版,不同发行版之间的联系和区别
  16. 异常:System.InvalidOperationException: This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms FIPS信息标准限值了MD5加密
  17. docker不能上传镜像到自己网站的仓库
  18. Concept Drift(概念漂移)
  19. 31. Next Permutation (java 字典序生成下一个排列)
  20. Use Laravel/homestead 环境维护基于 brophp 开发的老项目

热门文章

  1. 风暴英雄 http 302重定向 正在等待游戏模式下载完成
  2. Batch Normalization的算法本质是在网络每一层的输入前增加一层BN层(也即归一化层),对数据进行归一化处理,然后再进入网络下一层,但是BN并不是简单的对数据进行求归一化,而是引入了两个参数λ和β去进行数据重构
  3. Xcode的一些控制台命令
  4. Spring《二》 Bean的生命周期
  5. Java Servlet 配置
  6. Ztree自动触发第一个节点的点击事件
  7. web.xml中的url-pattern写法规则及匹配过程
  8. JAVA-mysql读写分离插件介绍
  9. Prime Distance POJ - 2689 线性筛
  10. 多进程Socket_Client