第三章、Spring Bean

第三章、Spring Bean

一、Spring Bean定义

​ 被称作 bean 的对象是构成应用程序的支柱也是由 Spring IoC 容器管理的。bean 是一个被实例化,组装,并通过 Spring IoC 容器所管理的对象。这些 bean 是由用容器提供的配置元数据创建的,例如,已经在先前章节看到的,在 XML 的表单中的 定义。

bean 定义包含称为配置元数据的信息,下述容器也需要知道配置元数据:

  • 如何创建一个 bean
  • bean 的生命周期的详细信息
  • bean 的依赖关系

在这里插入图片描述

二、Spring Bean属性:

属性描述
class这个属性是强制性的,并且指定用来创建 bean 的 bean 类。
name这个属性指定唯一的 bean 标识符。在基于 XML 的配置元数据中,你可以使用 ID 和/或 name 属性来指定 bean 标识符。
scope这个属性指定由特定的 bean 定义创建的对象的作用域,它将会在 bean 作用域的章节中进行讨论。
constructor-arg用来注入依赖关系。
properties用来注入依赖关系。
autowiring mode用来注入依赖关系。
lazy-initialization mode延迟初始化的 bean 告诉 IoC 容器在它第一次被请求时,而不是在启动时去创建一个 bean 实例。
initialization 方法在 bean 的所有必需的属性被容器设置之后,调用回调方法。它将会在 bean 的生命周期章节中进行讨论。
destruction 方法当包含该 bean 的容器被销毁时,使用回调方法。它将会在 bean 的生命周期章节中进行讨论。
factory-method指定创建bean的工厂方法。
factory-bean指定创建bean的工厂类对象,class属性将失效。
init-method创建一个bean之后调用该方法,初始化方法必须是一个无参方法。
destory-method在销毁bean之前可以执行指定的方法。

三、从IoC容器读取bean对象的三种方法

public class HelloService {
	public String sayHello(String name) {
		return "Hello " + name;
	}
}
<bean id="helloService" class="com.tjetc.service.HelloService"></bean>
<bean id="helloService2" class="com.tjetc.service.HelloService"></bean>
(1)容器对象.getBean(“bean的id”),精确定位,需要强制转换
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloService helloService = (HelloService) context.getBean("helloService");
(2)容器对象.getBean(“bean的id”,bean.class),精确定位,不需要强制转换
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloService helloService = context.getBean("helloService",HelloService.class);
(3)容器对象.getBean(bean.class),不需要强制转换
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloService helloService = context.getBean(HelloService.class);

四、Spring实例化bean的三种方法

(1)使用默认的构造方法创建bean对象

<bean id="helloService" class="bean的全路径名">

容器实例化会调用bean的默认无参数的构造方法

public class HelloService {
	public HelloService() {
		System.out.println("HelloService()构造方法...");
	}
	public String sayHello(String name) {
		return "Hello " + name;
	}
}
<bean id="helloService" class="com.tjetc.service.HelloService"></bean>
public static void main(String[] args) {
		// 创建Ioc容器
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		HelloService helloService = context.getBean("helloService",HelloService.class);
		String hello = helloService.sayHello("张三");
		System.out.println(hello);
	}

HelloService()构造方法...

Hello 张三

(2)静态工厂创建bean对象

<bean id="helloService" class="工厂的类的全路径" factory-method="工厂类创建对象的工厂方法">

工厂类:

public class HelloServiceFactory {
	public static HelloService createHelloService() {
		System.out.println("createHelloService() ...");
		return new HelloService();
	}
}

applicationContext.xml:

<bean id="helloService" class="com.tjetc.factory.HelloServiceFactory" factory-method="createHelloService"></bean>
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloService helloService = context.getBean("helloService",HelloService.class);
String hello = helloService.sayHello("张三");
System.out.println(hello);
(3)配置工厂类bean

helloService配置 factory-bean=”工厂类bean节点的id的值” factory-method=”工厂类创建对象的方法名”

<bean id="helloServiceFactory" class="com.tjetc.factory.HelloServiceFactory"></bean>
<bean id="helloService" factory-bean="helloServiceFactory" factory-method="createHelloService"></bean>
// 创建Ioc容器
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloService helloService = context.getBean("helloService",HelloService.class);
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值