Spring 实例化Bean的3种方式
创建Bean的实例有3种方式:
- 构造器方式
- 静态工厂方式
- 实例工厂方式
构造器方式
构造器方式是最简单,也是最常用的。
写一个Bean,
- 提供带参的构造器:使用带参的构造器创建bean的实例。
- 或者提供无参的构造器+setter方法:先使用无参的构造器创建对象,再调用setter方法注入依赖。
使用注解或xml文件配置bean,注入所需依赖。
此种方式是使用构造方法直接创建Bean的实例,不由工厂类负责生产Bean的实例。
静态工厂方式
需要创建一个工厂类,在工厂类中写一个静态方法,返回该Bean的一个实例。
1、写一个Bean,提供带参的构造器
public class Student {
private int no;
private String name;
private float score; public Student(int no, String name, float score) {
this.no = no;
this.name = name;
this.score = score;
} //......
}
2、写一个工厂类,用静态方法生产该Bean的实例
public class StudentFactory {
public static Student createStudent(int no,String name,float score) {
return new Student(no, name, score);
}
}
因为在工厂中使用的是静态方法来生产该Bean的实例,所以称为静态工厂方式。
一个工厂可以有多个静态方法,生产多个Bean的多种实例。
3、在xml文件中配置工厂类
<bean name="student" class="com.chy.utils.StudentFactory" factory-method="createStudent">
<constructor-arg index="0" value="1"/>
<constructor-arg index="1" value="chy"/>
<constructor-arg index="2" value="100"/>
</bean>
配置的是class="工厂类",因为是静态方法,直接通过工厂类调用,所以不需要工厂类的实例,也就不需要在xml文件中配置工厂类的实例。这个配置创建的是Student类的实例。
factory-method指定用哪个静态方法生产bean,<constructor-arg>给静态方法传入参数。
注解简化了配置,但注解只能进行简单的配置,复杂的配置还得在xml中配置。
通常注解、xml结合使用,简单的配置用注解,复杂的配置写在xml中。
4、使用
Student student = applicationContext.getBean("student", Student.class);
也可以使用空参构造器+setter方法注入,要麻烦些。只有1、2步不同:
1、提供空参构造器、setter方法
public class Student {
private int no;
private String name;
private float score; public void setNo(int no) {
this.no = no;
} public void setName(String name) {
this.name = name;
} public void setScore(float score) {
this.score = score;
} //......
}
2、先调用空参构造器创建实例,再调用setter方法赋值
public class StudentFactory {
public static Student createStudent(int no,String name,float score) {
Student student = new Student();
student.setNo(no);
student.setName(name);
student.setScore(score);
return student;
}
}
实例工厂方式
工厂中生产该Bean的方法不是静态的,在xml文件中需要同时配置工厂、该Bean。
1、写一个Bean,提供带参的构造器
2、写一个工厂类,用实例方法生产Bean(方法不用static修饰)
3、在xml文件中同时配置工厂类、该Bean
<bean name="studentFactory" class="com.chy.utils.StudentFactory" />
<bean name="student" class="com.chy.bean.Student" factory-bean="studentFactory" factory-method="createStudent">
<constructor-arg index="0" value="1"/>
<constructor-arg index="1" value="chy"/>
<constructor-arg index="2" value="100"/>
</bean>
因为没有使用static修饰方法,不能通过工厂类的类名来调用,只能通过工厂类的实例来调用,所以要配置工厂类的实例。
4、使用
Student student = applicationContext.getBean("student", Student.class);
同样可以使用空参构造器+setter方法的方式。
上面静态工厂、实例工厂的xml配置,都是直接生产目标类的实例。
创建实例需要传入实参,把实参写死在xml中,这显然不可取。上面的xml配置不常用。
一般只管理工厂类的实例:
<bean name="studentFactory" class="com.chy.utils.StudentFactory"/>
scope默认就是单例,不必显式配置。
在程序中通过工厂创建目标对象,实参可变化:
StudentFactory studentFactory = applicationContext.getBean("studentFactory", StudentFactory.class);
Student student = studentFactory.createStudent(1, "chy", 100);
静态工厂甚至不必进行配置,直接通过类名调用静态方法创建目标对象:
Student student = StudentFactory.createStudent(1, "chy", 100);
最新文章
- [LeetCode] Boom Enemy 炸弹人
- 企业 SOA 设计(2)–组件化产品开发平台
- Myeclipse Templates详解(一) —— Java模板基础
- guava学习--ratelimiter
- dedecms购物车商品添加删除数量改变方式变成ajax
- xml之基础了解
- 自学php笔记
- 创建oracle 密码文件
- Volley该框架使用了大量的请求图片
- cctype学习
- Linux常用操作命令(二)
- Redhat Linux 自动修改密码
- 精彩源于起点——2018年潍坊市首次青少年Python编程公开课
- hdu3555数位dp基础
- linux下安装前端程序员必备软件
- 集合或数组转成String字符串
- 替换iframe的内容
- kmeans笔记
- zabbix 自定义监控 排除带报错提示
- 关于ImportError: libssl.so.10: cannot open shared object file: No such file or directory unable to load app 0 (mountpoint=&#39;&#39;) (callable not found or import error)
热门文章
- 解决docx4j 变量替换 由于变量存在样式式或空白字符 导致替换失败问题
- protobufjs@6.8.8 postinstall: `node scripts/postinstall`
- [";Visual Studio快捷键"; ,";Vs";,";IDEA快捷键";]
- git clone 某个链接时候报错Initialized empty Git repository in 不能克隆
- 记录一次TraceId的问题
- phpspreadsheet 中文文档(七)技巧和诀窍
- SQL中merge into用法
- CVE-2019-16278-Nostromo Web Server远程代码执行
- nginx添加sticky模块-cookie保持会话
- 【Spring Boot学习之九】缓存支持