Spring中的Bean-Bean实例化的三种方式

面向对象的程序中,想要使用某个对象,就需要先实例化对象。同样,在Spring中,要想使用容器中的Bean,也需要实例化Bean。实例化Bean的方式有三种,分别是构造器实例化、静态工厂实例化和实例工厂实例化(其中最常用的是构造器实例化)。接下来演示一下三种实例化方法
在这里插入图片描述

构造器实例化(使用类的无参数构造创建)

目录:
在这里插入图片描述

  1. 首先在 com.itheima.instance.constructor 包中定义一个 Bean1
    Bean1.java:
package com.itheima.instance.constructor;

public class Bean1 {
	
}
  1. 在同一个包下创建一个 Spring 配置文件 bean1.xml,在配置文件中定义一个id为 bean1 的 Bean,并通过 class 属性指定对应的实现类为Bean1.
    Bean1.xml:
<?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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!-- 将指定类配置给Spring,让Spring创建其对象的实例 -->
	<bean id="bean1" class="com.itheima.instance.constructor.Bean1"></bean>
</beans>

id 为 在 xml 里的这个 bean的标识。 class 为xml 里的这个bean 绑定的java类(bean)的全路径(包名+类名)

这个标签会自动寻找 Bean1类中的无参数构造函数来创建对象

  1. 创建测试类InstanceTest.java,添加Test1进行测试
    InstanceTest.java
public class InstanceTest {
	@Test
	public void Test1() {
		//定义配置文件路径
		String xmlPath="com/itheima/instance/constructor/bean1.xml";
		//ApplicationContext 在加载配置文件时,对Bean进行实例化
		ApplicationContext applicationContext =new ClassPathXmlApplicationContext(xmlPath);
		Bean1 bean1 = (Bean1) applicationContext.getBean("bean1");
		System.out.println(bean1);
		//4.关闭appplicationContext
		((ConfigurableApplicationContext)applicationContext).close();
	}

运行结果:
在这里插入图片描述

2.静态工厂实例化(使用静态工厂创建)

目录:
在这里插入图片描述

  1. 在 com.itheima.instance.static_factory 包中创建Bean2
    Bean2.java:
package com.itheima.instance.static_factory;

public class Bean2 {

}
  1. 在同一个包中创建一个 MyBeanFactory 类,并在类中创建一个静态方法 createBean() 来返回 Bean2 实例。
package com.itheima.instance.static_factory;

public class MyBean2Factory {
	public static Bean2 createBean() {
		System.out.println("静态工厂实例化中......");
		return new Bean2();
	}
}
  1. 在同一个包下创建一个 Spring 配置文件 bean2.xml,先通过id定义一个bean2的Bean,由于使用的是静态工厂方法,class 属性指定的是工厂的实现类,在配置factory-method指定工厂方法创建bean
    bean2.xml:
<?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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">

	<bean id="bean2" class="com.itheima.instance.static_factory.MyBean2Factory" factory-method="createBean"></bean>
</beans>

id=“bean2” 表示创建的对象(bean)用 bean2 指代,使用到的 class (类)为 com.spring.demo 包下的 Bean2Factory 类(作为工厂类)

factory-method = "getBean2"表示调用 class 下的 getBean2方法来创建对象(而且factory-method指定的方法必须为static静态方法)

  1. 在测试类中添加Test2方法测试
@Test
	public void Test2() {
		//定义配置文件路径
		String xmlPath="com/itheima/instance/static_factory/bean2.xml";
		//ApplicationContext 在加载配置文件时,对Bean进行实例化
		ApplicationContext applicationContext =new ClassPathXmlApplicationContext(xmlPath);
		Bean2 bean2 = (Bean2) applicationContext.getBean("bean2");
		System.out.println(bean2);
		//4.关闭appplicationContext
		((ConfigurableApplicationContext)applicationContext).close();
	}

结果
在这里插入图片描述

实例工厂实例化(使用实例工厂创建)

目录:
在这里插入图片描述

  1. 在 com.itheima.instance.factory 包中创建 Bean3
package com.itheima.instance.factory;

public class Bean3 {

}
  1. 再创建一个MyBean3Factory ,类中创建一个方法 createBean() ,返回 Bean3 的实例。
    MyBean3Factory.java:
package com.itheima.instance.factory;

public class MyBean3Factory {
	public MyBean3Factory() {
		System.out.println("Bean3工厂实例化中......");
	}
	public Bean3 createBean() {
		return new Bean3();
	}
}

  1. 在同一个包下创建一个 Spring 配置文件 bean3.xml,先配置一个工厂实例,class指向MyBean3Factory 然后再配置一个bean3的实例,使用 factory_bean 属性指向配置的实例工厂,使用factory-method属性确定使用工厂中的那个方法.
    bean2.xml:
<?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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!-- 配置工厂 -->
	<bean id="myBean3Factory" class="com.itheima.instance.factory.MyBean3Factory"></bean>
	<!-- 使用 factory_bean 属性指向配置的实例工厂,使用factory-method属性确定使用工厂中的那个方法-->
	<bean id="bean3" factory-bean="myBean3Factory" factory-method="createBean"></bean>
</beans>
  1. 在测试类中添加测试方法
	@Test
	public void Test3() {
		//定义配置文件路径
		String xmlPath="com/itheima/instance/factory/bean3.xml";
		//ApplicationContext 在加载配置文件时,对Bean进行实例化
		ApplicationContext applicationContext =new ClassPathXmlApplicationContext(xmlPath);
		Bean3 bean3 = (Bean3) applicationContext.getBean("bean3");
		System.out.println(bean3);
		//4.关闭appplicationContext
		((ConfigurableApplicationContext)applicationContext).close();
	}

结果:
在这里插入图片描述

总结

三种方法最常用的就是构造器实例化,

静态工厂类与非静态工厂类的区别是,前者不需要创建对象,直接可以调用静态方法创建bean;后者则要先创建对象,然后再通过对象调用其方法创建bean

三种实例化方式的区别

构造器实例化:通过无参构造的方法实例化Bean,其实质是将Bean对应的类交给Spring自带的工厂(BeanFactory)管理,由Spring自带的工厂模式帮我们创建和维护这个类

静态工厂方式实例化:通过静态工厂创建并返回Bean。其实质是将Bean对应的类交给我们自己的静态工厂管理。Spring只是帮我们调用了静态工厂创建实例的方法。运用:很多时候我们在使用第三方jar包提供给我们的类时,由于这个类没有构造方法,是通过第三方包提供的静态工厂创建的,如果我们想把第三方jar包里面的这个类交由Spring来管理的话,就可以使用Spring提供的静态工厂创建实例的配置。

实例工厂方式实例化:通过实例工厂创建并返回Bean,其实质就是把创建实例的工厂类和调用工厂类的方法创建实例的方法的过程也交由Spring管理,创建实例的这个过程也是由我们自己配置的实例工厂内部实现的。运用:如Spring整合Hibernate就是通过这种方式实现的。但对于没有与Spring整合过的工厂类,我们一般都是自己用代码来管理的。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值