第三章、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);

猜你喜欢

转载自blog.csdn.net/yubo_830/article/details/106341442
今日推荐