当前位置:网站首页>Factorybean usage scenario
Factorybean usage scenario
2022-07-19 01:15:00 【xharvard】
Official website address : 1.8.3. Customizing Instantiation Logic with a FactoryBean
summary
FactoryBean It literally means factory Bean. It just acts as a factory . Its getObject() Method can produce another object , And the object of production , It's up to you Spring Container management .
adopt FactoryBean Implementation class of , Initial lowercase , What you get is getObject Returned object . Add a... Before the initial lowercase & Symbol , What you get is FactoryBean The instance itself .
Spring There are several FactoryBean Implementation class of . Used in Spring Of Bean Configure the corresponding set properties .
ListFactoryBean、MapFactoryBean、SetFactoryBean
Sample code
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 The role of : adopt ApplicationContext.getBean() What you get is Computer object
* If you want to get Machine In itself , Used when obtaining & + id
*
* @see ListFactoryBean
*
*/
public class Machine implements FactoryBean<Computer> {
@Override
public Computer getObject() throws Exception {
Computer computer = new Computer();
computer.setType(" The notebook ");
computer.setDisplay(" samsung ");
return computer;
}
@Override
public Class<?> getObjectType() {
return null;
}
}public class FactoryBeanDemo {
public static void main(String[] args) {
// load context.xml
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("META-INF/context-factorybean.xml");
Computer computer = context.getBean("machine", Computer.class);
System.out.println(computer);
// If you want to get Machine Bean In itself
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>边栏推荐
- 2022.7.1
- See the application of comparative learning in recommendation from 22 top meetings
- [deep learning] (II) basic learning notes of deep learning
- Problems in installing MySQL in CentOS
- Redis命令
- The difference between beanfactory and ApplicationContext
- P1321 单词覆盖还原【入门】
- pytorch index_select
- Swagger
- 2022年3月盗取微软源代码的 APT组织 lapsus$完整资料汇总
猜你喜欢
随机推荐
WampServer
Introduction to MySQL DLJD Lao Du
Oracle Database 12c 参数文件(Spfile 和 Pfile)
2022.7.1
Problems in installing MySQL in CentOS
20210519-LeetCode-双指针
MySQL常用命令
PHP upload pictures
Swagger related graphics
Solve the garbled code when inserting Chinese values in MySQL database
P1876 开灯【入门】
Lambda related graphics
Go BitSet of one library per day
The difference between beanfactory and ApplicationContext
(零七)Flask有手就行——初识数据库(Flask-SQLAlchemy)
C 1 to 100 sum 9*9 multiplication table grade ABCDE
Initial understanding of functions - Part 1
cli和vite通过代理实现跨域
Oracle自动存储管理18c分步安装-1
12 张图,带你彻底理解 ZGC垃圾收集器!
https://docs.spring.io/spring-framework/docs/5.2.22.RELEASE/spring-framework-reference/core.html#beans![[MySQL] Error 1130 problem solution](/img/75/6fcb13bbfa4f75615a1e7c2e317ca9.png)








