第二章 创建Bean的及Bean范围

实例化bean

示例:

public class Orange implements Fruit {
	private String name;

	
	public Orange() {
		super();
	}

	public Orange(String name) {
		super();
		this.name = name;
	}

	@Override
	public void eat() {
		System.out.println("吃桔子");
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

 


配置文件

 

<?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-2.5.xsd">
	<bean id="fruit" class="model.Orange" />
</beans>

 

Test 测试:

public class Test {
	public static void main(String[] args) {
		ApplicationContext acx = new ClassPathXmlApplicationContext(
				"chapter2.xml");
		Fruit fruit =(Fruit)acx.getBean("fruit");
		fruit.eat();
	}
}

 

 

Spring容器在实例化对象的时候,需要用到无参的构造方法.记住,提供一个无参的构造

 

 

 

使用静态工厂方法实例化

 

示例:

 

public class FruitFactory {
	public static Fruit getInstanceApple() {
		return new Apple();
	}

	public static Fruit getInstanceOrange() {
		return new Orange();
	}
	
	public static Fruit getInstance(int type) {
		if(type == 1){
			return new Apple();
		}else if (type == 2){
			return new Orange();
		}else{
			return null;
		}
	}
}

 

 配置文件

<?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-2.5.xsd">
	<bean id="apple" class="model.FruitFactory" factory-method="getInstanceApple" />
	<bean id="orange" class="model.FruitFactory" factory-method="getInstanceOrange" />
	<bean id="fruit" class="model.FruitFactory" factory-method="getInstance">
		<constructor-arg type="int" value="2" />
	</bean>
</beans>

 

  Test 测试:

public class Test {
	public static void main(String[] args) {
		ApplicationContext acx = new ClassPathXmlApplicationContext(
				"chapter2.xml");
		Apple apple =(Apple)acx.getBean("apple");
		apple.eat();
		Orange orange =(Orange)acx.getBean("orange");
		orange.eat();
		Fruit fruit =(Fruit)acx.getBean("fruit");
		fruit.eat();
	}
}

  

 

 

 

 

 使用实例工厂方法实例化

 

示例:
工厂类:

public class FruitFactory {
	public Fruit getInstanceApple() {
		return new Apple();
	}

	public Fruit getInstanceOrange() {
		return new Orange();
	}

	public Fruit getInstance(int type) {
		if (type == 1) {
			return new Apple();
		} else if (type == 2) {
			return new Orange();
		} else {
			return null;
		}
	}
}

 

 

 

 

 配置文件

  

<?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-2.5.xsd">
	<bean id="fruitFactory" class="model.FruitFactory" />

	<bean id="apple" factory-bean="fruitFactory" factory-method="getInstanceApple" />
	<bean id="orange" factory-bean="fruitFactory" factory-method="getInstanceOrange" />
	<bean id="fruit" factory-bean="fruitFactory" factory-method="getInstance">
		<constructor-arg type="int" value="2" />
	</bean>
</beans>

 

 

 

Test 测试:

 

 

public class Test {
	public static void main(String[] args) {
		ApplicationContext acx = new ClassPathXmlApplicationContext(
				"chapter2.xml");
		Apple apple =(Apple)acx.getBean("apple");
		apple.eat();
		Orange orange =(Orange)acx.getBean("orange");
		orange.eat();
		Fruit fruit =(Fruit)acx.getBean("fruit");
		fruit.eat();
	}
}

 

 

 

Bean的作用域

作用域

描述

singleton

在每个Spring IoC容器中一个bean定义对应一个对象实例。

prototype

一个bean定义对应多个对象实例。

request

在一次HTTP请求中,一个bean定义对应一个实例;即每次HTTP请求将会有各自的bean实例, 它们依据某个bean定义创建而成。该作用域仅在基于webSpring ApplicationContext情形下有效。

session

在一个HTTP Session中,一个bean定义对应一个实例。该作用域仅在基于webSpring ApplicationContext情形下有效。

global session

在一个全局的HTTP Session中,一个bean定义对应一个实例。典型情况下,仅在使用portlet context的时候有效。该作用域仅在基于webSpring ApplicationContext情形下有效。

 

singleton单例示例

<bean id="fruit" class="model.Apple" scope="singleton"/>

scope="singleton"是默认值,设于不设都可以,每次容器都会给同一个对象给你.

 

 

 

 

prototype非单例示例

<bean id="fruit" class="model.Apple" scope="prototype"/>
配置此范围后,每次向容器索取对象时,容器会创建一个新的对象给你.

 

 

 

Bean的生命周期

 

 

 

      范围为:scope="Singleton"的情况
      容器启动时,就实例化
 

 

 

范围为:scope="prototype"的情况
  什么时候用,什么时候才实例化

 

 


 延迟初始化bean
lazy-init="true"
lazy-init="false"

<bean id="fruit" class="model.Apple" lazy-init="false" />

 不要和scope属性一起使用.
true:延时,不立即创建
false:不延时,立即创建

 

 

类初始化和销毁自动调用的方法

 

 

 

 

<bean id="fruit" class="model.Apple" init-method="init"
		destroy-method="destroy" />

 

init-method="init":指定方法名,在对象实例化以后,自动调用
destroy-method="destroy":指定方法名,在容器关闭以后,自动调用

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值