当前位置:网站首页>FactoryBean 使用场景
FactoryBean 使用场景
2022-07-17 00:01:00 【xharvard】
官网地址: 1.8.3. Customizing Instantiation Logic with a FactoryBean
概述
FactoryBean字面意思是工厂Bean。它就是起到一个工厂的作用。其getObject()方法可以生产另外一个对象,且生产的对象,也交由Spring容器管理。
通过FactoryBean的实现类,首字母小写,获得的是getObject返回的对象。在首字母小写前面加一个&符号,获得的是FactoryBean实例本身。
Spring提供了几个FactoryBean的实现类。用于在Spring的Bean中配置相应集合属性。
ListFactoryBean、MapFactoryBean、SetFactoryBean
示例代码
public class Computer {
private String type;
private String display;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getDisplay() {
return display;
}
public void setDisplay(String display) {
this.display = display;
}
@Override
public String toString() {
return "Computer{" + "type='" + type + '\'' + ", display='" + display + '\'' + '}';
}
}/**
* FactoryBean 的作用:通过ApplicationContext.getBean() 获取到的是Computer对象
* 如果要获取Machine本身,在获取是用 & + id
*
* @see ListFactoryBean
*
*/
public class Machine implements FactoryBean<Computer> {
@Override
public Computer getObject() throws Exception {
Computer computer = new Computer();
computer.setType("笔记本");
computer.setDisplay("三星");
return computer;
}
@Override
public Class<?> getObjectType() {
return null;
}
}public class FactoryBeanDemo {
public static void main(String[] args) {
// 加载context.xml
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("META-INF/context-factorybean.xml");
Computer computer = context.getBean("machine", Computer.class);
System.out.println(computer);
//如果要获取Machine Bean本身
Machine machine = context.getBean("&machine", Machine.class);
System.out.println(machine);
}
}<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
<!-- bean definitions here -->
<bean id="machine" class="org.xharvard.ioc.factorybean.Machine"/>
</beans>边栏推荐
- 按住 Day 27 :服务
- MySQL lock: comprehensive understanding
- Which is the best bag grabbing tool (violent theft foreplay)
- “husky install“ command already exists in prepare script, skipping./Users/982471938qq.com/Desktop
- Rocky basic exercises find, SED, grep, tar
- 那一朵最美的黄花
- P1876 开灯【入门】
- [Shader implémente l'effet Wave Shader chapitre 1]
- Flowable workflow (flowable database table structure)
- 12 张图,带你彻底理解 ZGC垃圾收集器!
猜你喜欢

Computer Graphics From Scratch - Chapter 3

mysql插入数据会失败?为什么?

MySQL lock: comprehensive understanding

Hcia-r & s self use notes (8) IP address basis, subnet mask, subnet Division

“husky install“ command already exists in prepare script, skipping./Users/982471938qq.com/Desktop

Google Chrome OS officially changed its name to Chrome OS brand

Acquisition of network security B module coding information of national vocational college skills competition

Hcia-r & s self use notes (6) (network layer) ICMP, IP protocol foundation and fragmentation

【着色器实现Wave效果_Shader效果第一篇】

Hcia-r & s self use notes (10) VRP foundation, command, remote management
随机推荐
Greenplum6.x客户端连接
Mysql—锁:全面理解
基于yoloV5的.pt模型转换为.engine模型,并通过Deepstream加速,可多相机实时传输,采用nvidia的Xavier板子(精一)
Baidu page starts from 1 and JPA starts from 0
Sqli labs less-1 (extractvalue of error injection)
1764. 通过连接另一个数组的子数组得到一个数组 ●●
NVIDIA's Jetson uses deepstream to accelerate common problem analysis, deepstream messages are sent and received externally, xvaier self startup program, and excellent blog summary (fine 2)
Mysql - contrôle de la concurrence Multi - versions (mvcc)
[Shader implémente l'effet Wave Shader chapitre 1]
“husky install“ command already exists in prepare script, skipping./Users/982471938qq.com/Desktop
软核微处理器
IDEA开发Servlet项目 如何右键创建servlet
高等数学---第八章多元函数微分学---多元函数的极值与最值
UVA11362 Phone List 题解
常见链表题及其 Go 实现
【深度学习】(二)深度学习基础学习笔记
抖音抓包通杀版-version21.5
How to solve the over correlation of graph neural network? A new perspective of IBM!
Rocky基础练习题-find、sed、grep、tar
【日常训练】剑指 Offer II 041. 滑动窗口的平均值
https://docs.spring.io/spring-framework/docs/5.2.22.RELEASE/spring-framework-reference/core.html#beans