创建Bean的实例有3种方式:

  • 构造器方式
  • 静态工厂方式
  • 实例工厂方式

构造器方式

构造器方式是最简单,也是最常用的。

写一个Bean,

  • 提供带参的构造器:使用带参的构造器创建bean的实例。
  • 或者提供无参的构造器+setter方法:先使用无参的构造器创建对象,再调用setter方法注入依赖。

使用注解或xml文件配置bean,注入所需依赖。

此种方式是使用构造方法直接创建Bean的实例,不由工厂类负责生产Bean的实例。


静态工厂方式

需要创建一个工厂类,在工厂类中写一个静态方法,返回该Bean的一个实例。

1、写一个Bean,提供带参的构造器

  1. public class Student {
  2. private int no;
  3. private String name;
  4. private float score;
  5.  
  6. public Student(int no, String name, float score) {
  7. this.no = no;
  8. this.name = name;
  9. this.score = score;
  10. }
  11.  
  12. //......
  13. }

2、写一个工厂类,用静态方法生产该Bean的实例

  1. public class StudentFactory {
  2. public static Student createStudent(int no,String name,float score) {
  3. return new Student(no, name, score);
  4. }
  5. }

因为在工厂中使用的是静态方法来生产该Bean的实例,所以称为静态工厂方式。

一个工厂可以有多个静态方法,生产多个Bean的多种实例。

3、在xml文件中配置工厂类

  1. <bean name="student" class="com.chy.utils.StudentFactory" factory-method="createStudent">
  2. <constructor-arg index="0" value="1"/>
  3. <constructor-arg index="1" value="chy"/>
  4. <constructor-arg index="2" value="100"/>
  5. </bean>

配置的是class="工厂类",因为是静态方法,直接通过工厂类调用,所以不需要工厂类的实例,也就不需要在xml文件中配置工厂类的实例。这个配置创建的是Student类的实例。

factory-method指定用哪个静态方法生产bean,<constructor-arg>给静态方法传入参数。

注解简化了配置,但注解只能进行简单的配置,复杂的配置还得在xml中配置。

通常注解、xml结合使用,简单的配置用注解,复杂的配置写在xml中。

4、使用

  1. Student student = applicationContext.getBean("student", Student.class);

也可以使用空参构造器+setter方法注入,要麻烦些。只有1、2步不同:

1、提供空参构造器、setter方法

  1. public class Student {
  2. private int no;
  3. private String name;
  4. private float score;
  5.  
  6. public void setNo(int no) {
  7. this.no = no;
  8. }
  9.  
  10. public void setName(String name) {
  11. this.name = name;
  12. }
  13.  
  14. public void setScore(float score) {
  15. this.score = score;
  16. }
  17.  
  18. //......
  19. }

2、先调用空参构造器创建实例,再调用setter方法赋值

  1. public class StudentFactory {
  2. public static Student createStudent(int no,String name,float score) {
  3. Student student = new Student();
  4. student.setNo(no);
  5. student.setName(name);
  6. student.setScore(score);
  7. return student;
  8. }
  9. }

实例工厂方式

工厂中生产该Bean的方法不是静态的,在xml文件中需要同时配置工厂、该Bean。

1、写一个Bean,提供带参的构造器

2、写一个工厂类,用实例方法生产Bean(方法不用static修饰)

3、在xml文件中同时配置工厂类、该Bean

  1. <bean name="studentFactory" class="com.chy.utils.StudentFactory" />
  2. <bean name="student" class="com.chy.bean.Student" factory-bean="studentFactory" factory-method="createStudent">
  3. <constructor-arg index="0" value="1"/>
  4. <constructor-arg index="1" value="chy"/>
  5. <constructor-arg index="2" value="100"/>
  6. </bean>

因为没有使用static修饰方法,不能通过工厂类的类名来调用,只能通过工厂类的实例来调用,所以要配置工厂类的实例。

4、使用

  1. Student student = applicationContext.getBean("student", Student.class);

同样可以使用空参构造器+setter方法的方式。


上面静态工厂、实例工厂的xml配置,都是直接生产目标类的实例。

创建实例需要传入实参,把实参写死在xml中,这显然不可取。上面的xml配置不常用。

一般只管理工厂类的实例:

  1. <bean name="studentFactory" class="com.chy.utils.StudentFactory"/>

scope默认就是单例,不必显式配置。

在程序中通过工厂创建目标对象,实参可变化:

  1. StudentFactory studentFactory = applicationContext.getBean("studentFactory", StudentFactory.class);
  2. Student student = studentFactory.createStudent(1, "chy", 100);

静态工厂甚至不必进行配置,直接通过类名调用静态方法创建目标对象:

  1. Student student = StudentFactory.createStudent(1, "chy", 100);

Spring 实例化Bean的3种方式的更多相关文章

  1. Spring实例化Bean的三种方式及Bean的类型

    1.使用类构造器实例化  [默认的类构造器] <bean id=“orderService" class="cn.itcast.OrderServiceBean"/ ...

  2. Spring 实例化bean的三种方式

    第一种方法:直接配置Bean <bena id="所需要实例化的一个实例名称" class="包名.类名"/> 例如: 配置文件中的bean.XML ...

  3. Spring实例化bean的几种方式

    一,通过constructor实例化bean Spring可以实例化各种类型的类,不要求必须是JavaBean类型的类.在XML中配置类如下: <bean id="exampleBea ...

  4. Spring 实例化bean的三种方式:

    方法一:使用构造器实例化bean java代码: package com.model; public class User { private String username; public User ...

  5. spring实例化bean的三种方式

    公共使用的实体

  6. Spring学习之实例化bean的三种方式

    实例化bean的三种方式 构造器实例化bean Person.java public class Person { private String name; private Integer age; ...

  7. spring创建bean的三种方式

    spring创建bean的三种方式: 1通过构造方法创建bean(最常用) 1.1 spring默认会通过无参构造方法来创建bean,如果xml文件是这样配置,则实体类中必须要有无参构造方法,无参构造 ...

  8. spring 注入bean的两种方式

    我们都知道,使用spring框架时,不用再使用new来实例化对象了,直接可以通过spring容器来注入即可. 而注入bean有两种方式: 一种是通过XML来配置的,分别有属性注入.构造函数注入和工厂方 ...

  9. Spring获取bean的几种方式

    工作中需要对一个原本加载属性文件的工具类修改成对数据库的操作当然,ado层已经写好,但是需要从Spring中获取bean,然而,工具类并没有交给Spring来管理,所以需要通过方法获取所需要的bean ...

随机推荐

  1. lastlogon

    function Get-ADUserLastLogon($userName) { $dcs = Get-ADDomainController -Filter {Name -like "*& ...

  2. 支付宝小程序开发——获取位置API没有城市区号的最佳处理方案

    前言: 需要对城市区号进行判断,但是支付宝小程序提供的my.getLocation() API返回的数据中只有6位的城市行政代码,诸如:深圳(440300),并没有区号(0755),那么怎么办呢? 需 ...

  3. shell (7)if 表达式

    文件表达式if [ -f file ] 如果文件存在if [ -d … ] 如果目录存在if [ -s file ] 如果文件存在且非空if [ -r file ] 如果文件存在且可读if [ -w ...

  4. linux环境下安装python 3

    说明: 在linux环境下,都默认安装python 2的环境,由于python3在python2的基础上升级较大,所以安装python 3环境用于使用最新的python 3的语法. 安装过程: 1.下 ...

  5. html css 浮层 侧边栏

    2019-7-1 16:02:25 星期一 实现的效果是点击, 然后从左侧滑出, 再点击, 就滑进去 <!DOCTYPE HTML> <html lang="en" ...

  6. HTML和CSS个人笔记

    目录 定位 文字显示在图片上 ul的li元素的小圆点换成图片 关于Bootstrap的响应式 不要在container之外使用row 不要使用padding的时候固定高度 不要使用<hr p标签 ...

  7. 基于Spring Boot架构的前后端完全分离项目API路径问题

    最近的一个项目采用前后端完全分离的架构,前端组件:vue + vue-router + vuex + element-ui + axios,后端组件:Spring Boot + MyBatis.之所以 ...

  8. python-mysql事务

    MySQL 事务 MySQL 事务主要用于处理操作量大,复杂度高的数据.简单的理解就是:完成一件事情的多个mysql语句的集合就是一个事务了,可能有人会想,我的mysql本来就是一句一句语句执行的啊, ...

  9. visual studio ------- 更改字体和背景颜色

    1.打开vs   点击工具  选择选项 2.想要更换主题的也可以更换主题, 3.更改字体 4.更改为护眼小背景   参数为    85   123  205 ee

  10. springboot2.0以后的junit

    只需要引入: <dependency> <groupId>junit</groupId> <artifactId>junit</artifactI ...