spring学习笔记1-spring Ioc容器-Spring Bean

spring学习笔记1-spring Ioc容器-Spring Bean

Bean的定义
被称作 bean 的对象是构成应用程序的支柱也是由 Spring IoC 容器管理的。bean 是一个被实例化,组装,并通过 Spring IoC 容器所管理的对象。这些 bean 是由用容器提供的配置元数据创建的。

每个Bean的定义包含下列属性

属性描述
class该属性是强制的,并且指定用来创建 bean 的 bean 类。
name这个属性指定唯一的 bean 标识符。
Scope这个属性指定由特定的 bean 定义创建的对象的作用域,
constructor-arg它是用来注入依赖关系的
properties它是用来注入依赖关系的
autowiring mode它是用来注入依赖关系的
lazy-initialization mode延迟初始化的 bean 告诉 IoC 容器在它第一次被请求时,而不是在启动时去创建一个 bean 实例。
initialization 方法在 bean 的所有必需的属性被容器设置之后,调用回调方法。
destruction 方法当包含该 bean 的容器被销毁时,使用回调方法。

Spring Bean的作用域

当在 Spring 中定义一个 bean 时,你必须声明该 bean 的作用域的选项。例如,为了强制 Spring 在每次需要时都产生一个新的 bean 实例,你应该声明 bean 的作用域的属性为 prototype。同理,如果你想让 Spring 在每次需要时都返回同一个bean实例,你应该声明 bean 的作用域的属性为 singleton

Spring 框架支持以下五个作用域,分别为singleton、prototype、request、session和global session,5种作用域说明如下所示

注意,如果你使用 web-aware ApplicationContext 时,其中三个是可用的。

作用域描述
singleton在spring IoC容器仅存在一个Bean实例,Bean以单例方式存在,默认值
prototype每次从容器中调用Bean时,都返回一个新的实例,即每次调用getBean()时,相当于执行newXxxBean()
request每次HTTP请求都会创建一个新的Bean,该作用域仅适用于WebApplicationContext环境
session同一个HTTP Session共享一个Bean,不同Session使用不同的Bean,仅适用于WebApplicationContext环境
global-session一般用于Portlet应用环境,该运用域仅适用于WebApplicationContext环境

Singleton作用域
singleton 是默认的作用域,也就是说,当定义 Bean 时,如果没有指定作用域配置项,则 Bean 的作用域被默认为 singleton。
当一个bean的作用域为Singleton,那么Spring IoC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会返回bean的同一实例。

也就是说,当将一个bean定义设置为singleton作用域的时候,Spring IoC容器只会创建该bean定义的唯一实例。

Singleton是单例类型,就是在创建起容器时就同时自动创建了一个bean的对象,不管你是否使用,他都存在了,每次获取到的对象都是同一个对象。注意,Singleton作用域是Spring中的缺省作用域。

实例:

  1. 创建一个名为IocTest的Java工程
  2. 在工程中导入spring库文件。
  3. 在src下创建名为org.TestIoc03的包,在包下创建两个class文件,分别是TestIoc03.java和Tstudent.java
  4. 在src下创建名为Beans.xml的Bean配置文件

下面是文件TestIoc03.java的内容

package org.TestIoc03;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestIoc03 {

	public static void main(String[] args) {
		ApplicationContext context1 = new ClassPathXmlApplicationContext("Beans.xml");
		Tstudent student1 = (Tstudent)context1.getBean("Tstudent03");
		student1.helloStu();
		//第二次获取Tstudent
		Tstudent student2 = (Tstudent)context1.getBean("Tstudent03");
		student2.setName("ls");
		student2.helloStu();
		//再打印一次student1中的helloStu()方法
		student1.helloStu();
	}
}

下面是文件Tstudent.java的内容

package org.TestIoc03;

public class Tstudent {
	private String name;
	public void setName(String name) {
		this.name = name;
	}
	
	public void helloStu() {
		System.out.println("hello student "+name+" .");
	}
}

下面是文件Beans.xml的内容,其中前两个Bean是之前的实例创建的Bean

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="Tstudent01" class="org.TestIoc01.Tstudent">
			<property name="iocInfo" value="BeanFactory"></property>
	</bean>
	
	<bean id="Tstudent02" class="org.TestIoc02.Tstudent">
			<property name="iocInfo" value="ApplicationContext"></property>
	</bean>
	
	<bean id="Tstudent03" class="org.TestIoc03.Tstudent" scope="singleton">
			<property name="name" value="zs"></property>
	</bean>
</beans>

输出结果如下:
在这里插入图片描述

prototype作用域
当一个bean的作用域为Prototype,表示一个bean定义对应多个对象实例。Prototype作用域的bean会导致在每次对该bean请求(将其注入到另一个bean中,或者以程序的方式调用容器的getBean()方法)时都会创建一个新的bean实例。Prototype是原型类型,它在我们创建容器的时候并没有实例化,而是当我们获取bean的时候才会去创建一个对象,而且我们每次获取到的对象都不是同一个对象。

实例:

  1. 创建一个名为IocTest的Java工程
  2. 在工程中导入spring库文件。
  3. 在src下创建名为org.TestIoc04的包,在包下创建两个class文件,分别是TestIoc04.java和Tstudent.java
  4. 在src下创建名为Beans.xml的Bean配置文件

下面是文件TestIoc04.java的内容

package org.TestIoc04;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestIoc04 {

	public static void main(String[] args) {
		ApplicationContext context1 = new ClassPathXmlApplicationContext("Beans.xml");
		Tstudent student1 = (Tstudent)context1.getBean("Tstudent04");
		student1.helloStu();
		
		Tstudent student2 = (Tstudent)context1.getBean("Tstudent04");
		student2.setName("ls");
		student2.helloStu();
		
		student1.helloStu();
		
	}

}

下面是文件Tstudent.java的内容

package org.TestIoc04;

public class Tstudent {
	private String name;
	public void setName(String name) {
		this.name=name;
	}
	
	public void helloStu() {
		System.out.println("hello student "+name+" .");
	}
}

下面是文件Beans.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="Tstudent01" class="org.TestIoc01.Tstudent">
			<property name="iocInfo" value="BeanFactory"></property>
	</bean>
	
	<bean id="Tstudent02" class="org.TestIoc02.Tstudent">
			<property name="iocInfo" value="ApplicationContext"></property>
	</bean>
	
	<bean id="Tstudent03" class="org.TestIoc03.Tstudent" scope="singleton">
			<property name="name" value="zs"></property>
	</bean>
	
	<bean id="Tstudent04" class="org.TestIoc04.Tstudent" scope="prototype">
			<property name="name" value="zs"></property>
	</bean>
</beans>

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值