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.运行结果
不变
spring中bean标签factory-method和factory-bean)详解工厂方法(factory-method和factory-bean)的更多相关文章
- Spring中的注入方式 和使用的注解 详解
注解:http://www.cnblogs.com/liangxiaofeng/p/6390868.html 注入方式:http://www.cnblogs.com/java-class/p/4727 ...
- Angular中innerHTML标签的样式不起作用详解
1.背景 在最近angular的项目中,需要用到[innerHTML]标签来指定一个div的样式: //HTML部分 <div class="contents" [inner ...
- 简单工厂,Factory Method(工厂方法)和Abstract Factory(抽象工厂)模式
对于简单工厂来说,它的工厂只能是这个样子的 public class SimplyFactory { /** * 静态工厂方法 */ public static Prouct factory(Str ...
- Spring事务Transaction配置的五种注入方式详解
Spring事务Transaction配置的五种注入方式详解 前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识.通过这次的学 ...
- 转载~kxcfzyk:Linux C语言多线程库Pthread中条件变量的的正确用法逐步详解
Linux C语言多线程库Pthread中条件变量的的正确用法逐步详解 多线程c语言linuxsemaphore条件变量 (本文的读者定位是了解Pthread常用多线程API和Pthread互斥锁 ...
- Android中Intent传值与Bundle传值的区别详解
Android中Intent传值与Bundle传值的区别详解 举个例子我现在要从A界面跳转到B界面或者C界面 这样的话 我就需要写2个Intent如果你还要涉及的传值的话 你的Intent就要写两 ...
- ORACLE中RECORD、VARRAY、TABLE的使用详解(转)
原文地址:ORACLE中RECORD.VARRAY.TABLE的使用详解
- Java集合中List,Set以及Map等集合体系详解
转载请注明出处:Java集合中List,Set以及Map等集合体系详解(史上最全) 概述: List , Set, Map都是接口,前两个继承至collection接口,Map为独立接口 Set下有H ...
- 对python3中pathlib库的Path类的使用详解
原文连接 https://www.jb51.net/article/148789.htm 1.调用库 ? 1 from pathlib import 2.创建Path对象 ? 1 2 3 4 5 ...
随机推荐
- 积跬步,聚小流------java信息生成图片
需求: 是在做证书的时候碰到的这个问题. 当时需求是能够进行在线打印证书,第一次进行的操作是直接打印html,并且已经排好版(用jqprint插件)进行打印.在打印时碰到了兼容的问题,另外因为背景图片 ...
- bzoj1924: [Sdoi2010]所驼门王的宝藏
陈年老题又来水一发啊啊啊 构图狗了一点,然后其实强连通缩点dij找最长路就没了. 没调出来有点气,直接打了第9个点的表.... 来逛blog的你教教我呗 #include<cstdio> ...
- javascript中的闭包以及闭包应用
闭包简单理解就是能够读取其他函数内部变量的函数,而在javascript中只有内部函数可以读取函数的内部变量,所以我们学习javascript时可以这样理解,函数A中嵌套了一个函数B,然后我们用函数B ...
- 16.QT鼠标
头文件 #include <QMouseEvent> #include <QStatusBar> #include <QLabel> protected: //鼠标 ...
- caffe mnist实例 --lenet_train_test.prototxt 网络配置详解
1.mnist实例 ##1.数据下载 获得mnist的数据包,在caffe根目录下执行./data/mnist/get_mnist.sh脚本. get_mnist.sh脚本先下载样本库并进行解压缩,得 ...
- Python的四个内置数据类型list, tuple, dict, set
Python语言简洁明了,可以用较少的代码实现同样的功能.这其中Python的四个内置数据类型功不可没,他们即是list, tuple, dict, set.这里对他们进行一个简明的总结. List ...
- Spark的数据存储(十九)
Spark本身是基于内存计算的架构,数据的存储也主要分为内存和磁盘两个路径.Spark本身则根据存储位置.是否可序列化和副本数目这几个要素将数据存储分为多种存储级别.此外还可选择使用Tachyon来管 ...
- 不得了,微软原生提供 AI 人工智能 API,而且面向网页开放
微软原生人工智能(AI) API 不得了,微软原生提供 AI 人工智能 API,而且面向网页开放
- 3ds Max制作欧式风格的墙壁路灯效果
在这篇文章中,我将解释我创建我的形象元宵节的步骤.我只是在寻找一个很好的参考图像在互联网上的东西,我觉得我想要的模型,这个形象.我发现了一个巨大的灯笼形象,但在白天的图片拍摄.我想改变我的形象和显示的 ...
- ItChat与图灵机器人的结合
前景: 我在知乎关注一位大佬 名字叫 LittleCoder 我是在他开发ItChat包时关注的 ItChat已经完成了微信的个人账号的API接口 已经实现了实时获取用户的即时信息并自动化进行回应 后 ...