第二章 创建bean及bean的范围

一:实例化bean

    构造方法实例化   

package com.spring.chapter2.service;

public interface Fruit {
	public void eat();
}

 

package com.spring.chapter2.service.impl;

import com.spring.chapter2.service.Fruit;

public class Apple implements Fruit {
	public void eat() {
		System.out.println("吃苹果!");
	}
}

 

package com.spring.chapter2.service.impl;

import com.spring.chapter2.service.Fruit;

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;
	}
}

     配置文件: //chapter2.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-2.5.xsd">
	<bean id="fruit" class="com.spring.chapter2.service.impl.Orange" />
</beans>

   测试类:  

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;
		}
	}
}

   配置文件: //chapter2.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-2.5.xsd">
	<bean id="apple" class="com.spring.chapter2.model.FruitFactory" factory-method="getInstanceApple" />
	<bean id="orange" class="com.spring.chapter2.model.FruitFactory" factory-method="getInstanceOrange" />
	<bean id="fruit" class="com.spring.chapter2.model.FruitFactory" factory-method="getInstance">
		<constructor-arg type="int" value="2" />
	</bean>
</beans>

   测试类:  

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定义创建而成。该作用域仅在基于web的Spring ApplicationContext情形下有效。
session 在一个HTTP Session中,一个bean定义对应一个实例。该作用域仅在基于web的Spring ApplicationContext情形下有效。
global session在一个全局的HTTP Session中,一个bean定义对应一个实例。典型情况下,仅在使用portlet context的时候有效。该作用域仅在基于web的Spring ApplicationContext情形下有效。

  singleton单例示例

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

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

  prototype非单例示例

     <bean id="fruit" class="model.Apple" 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、付费专栏及课程。

余额充值