SpringBean的生命周期
<!-- bean的生命周期: 单例对象: 将创建的bean放入容器 出生:容器创建,对象出生 配置文件加载完毕后,会立即创建bean对象,并存放到IOC容器中 活着:只要容器在,对象就一直活着 死亡:容器销毁,对象消亡 特点:生命周期与容器相同 --> <!-- 单例对象: init-method: 初始化方法 destroy-method: 销毁时调用的方法 --> <!--<bean scope="singleton" init-method="initMethod" destroy-method="destoryMethod" id="AccountService5" class="com.village.service.impl.AccountServiceImpl"></bean> --> <!-- 多实例bean的生命周期: 创建时机: 从IOC容器中获取对象时 销毁时机: 等待GC回收 --> <!-- 多例对象: 出生:每次使用时创建 活着:在使用过程中 死亡:使用完成之后,等待垃圾回收器回收。 多实例bean对象的生命周期 scope的值必须为: prototype --> <bean scope="prototype" init-method="initMethod" destroy-method="destoryMethod" id="AccountService6" class="com.village.service.impl.AccountServiceImpl"></bean>
package com.village.web; import com.village.service.AccountService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class BeanLifeClient { public static void main(String[] args) { /*ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); AccountService service = ac.getBean("AccountService5", AccountService.class); service.save();; // 关闭IOC容器 ((ClassPathXmlApplicationContext) ac).close();*/ ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); AccountService service = ac.getBean("AccountService6", AccountService.class); service.save();; // 关闭IOC容器 ((ClassPathXmlApplicationContext) ac).close(); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!