实例化,执行,初始化,销毁bean对象

本文介绍如何在Spring框架中实现懒加载功能,通过配置文件设置属性lazy-init=true来延迟实例化对象,仅在需要使用时创建。同时展示了ExampleBean类实例化、初始化及销毁的过程,并通过测试案例验证懒加载的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.修改applicationContext.xml,通过设置配置文件属性lazy-init="true",可以改变Spring容器创建对象的时机。
2.在顶级的<beans/>元素中的default-lazy-init属性,可以为容器所有<bean>指定延迟实例化特性

3.从输出结果可以看出,当使用ExampleBean对象时,才被创建,即,设置lazy-init="true"属性后,对象不使用不创建。


package org.spring.dao;

public class ExampleBean {
	
	public ExampleBean() {
		System.out.println("实例化ExampleBean");
	}

	public void execute() {
		System.out.println("执行ExampleBean处理");
	}

	public void init() {
		System.out.println("初始化ExampleBean对象");
	}

	public void destroy() {
		System.out.println("销毁ExampleBean对象");
	}

}

package org.spring.dao;

public class ExampleBean1 {
	public ExampleBean1() {
		System.out.println("实例化ExampleBean1");
	}
}


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd"
	default-lazy-init="true" 
	default-init-method="init"
	default-destroy-method="destroy">


	<bean id="exampleBean" class="org.spring.dao.ExampleBean" scope="singleton"
		init-method="init" destroy-method="destroy" depends-on="bean1" />
		
	<bean id="bean1" class="org.spring.dao.ExampleBean1" lazy-init="true" />

</beans>


package org.spring.test;

import org.junit.Test;
import org.spring.dao.ExampleBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class testCase {

	@Test
	public void testExampleBean() {
		// 实例化Spring容器示例
		String conf = "applicationContext.xml";
		ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
		//System.out.println(ac);
		// 获取ExampleBean对象
		ExampleBean bean1 = ac.getBean("exampleBean", ExampleBean.class);
		ExampleBean bean2 = ac.getBean("exampleBean", ExampleBean.class);
		System.out.println(bean1 == bean2);
		// 关闭Spring容器, 注意AbstractApplicationContext类型定义了 close()方法
		AbstractApplicationContext ctx = (AbstractApplicationContext) ac;
		ctx.close();
	}
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值