登录
注册
开源
企业版
高校版
搜索
帮助中心
使用条款
关于我们
开源
企业版
高校版
私有云
模力方舟
AI 队友
登录
注册
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
取消
前往登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
Watch
不关注
关注所有动态
仅关注版本发行动态
关注但不提醒动态
3
Star
47
Fork
23
DreamCoders
/
CoderGuide
代码
Issues
1169
Pull Requests
0
Wiki
统计
流水线
服务
JavaDoc
PHPDoc
质量分析
Jenkins for Gitee
腾讯云托管
腾讯云 Serverless
悬镜安全
阿里云 SAE
Codeblitz
SBOM
开发画像分析
我知道了,不再自动展开
更新失败,请稍后重试!
移除标识
内容风险标识
本任务被
标识为内容中包含有代码安全 Bug 、隐私泄露等敏感信息,仓库外成员不可访问
你能说说Spring框架中Bean的生命周期吗?
待办的
#IAJKSK
陌生人
拥有者
创建于
2024-08-13 10:05
<p style="text-align: justify;"><span style="color: rgb(255, 0, 0); font-size: 19px;"><strong>首先简单说一下(以下为一个回答的参考模板)</strong></span></p><p style="text-align: start;">1、实例化一个Bean--也就是我们常说的new;</p><p style="text-align: start;">2、按照Spring上下文对实例化的Bean进行配置--也就是IOC注入;</p><p style="text-align: start;">3、如果这个Bean已经实现了BeanNameAware接口,会调用它实现的setBeanName(String)方法,此处传递的就是Spring配置文件中Bean的id值</p><p style="text-align: start;">4、如果这个Bean已经实现了BeanFactoryAware接口,会调用它实现的setBeanFactory(setBeanFactory(BeanFactory)传递的是Spring工厂自身(可以用这个方式来获取其它Bean,只需在Spring配置文件中配置一个普通的Bean就可以);</p><p style="text-align: start;">5、如果这个Bean已经实现了ApplicationContextAware接口,会调用setApplicationContext(ApplicationContext)方法,传入Spring上下文(同样这个方式也可以实现步骤4的内容,但比4更好,因为ApplicationContext是BeanFactory的子接口,有更多的实现方法);</p><p style="text-align: start;">6、如果这个Bean关联了BeanPostProcessor接口,将会调用postProcessBeforeInitialization(Object obj, String s)方法,BeanPostProcessor经常被用作是Bean内容的更改,并且由于这个是在Bean初始化结束时调用那个的方法,也可以被应用于内存或缓存技术;</p><p style="text-align: start;">7、如果Bean在Spring配置文件中配置了init-method属性会自动调用其配置的初始化方法。</p><p style="text-align: start;">8、如果这个Bean关联了BeanPostProcessor接口,将会调用postProcessAfterInitialization(Object obj, String s)方法、;</p><blockquote style="text-align: start;">注:以上工作完成以后就可以应用这个Bean了,那这个Bean是一个Singleton的,所以一般情况下我们调用同一个id的Bean会是在内容地址相同的实例,当然在Spring配置文件中也可以配置非Singleton,这里我们不做赘述。</blockquote><p style="text-align: start;">9、当Bean不再需要时,会经过清理阶段,如果Bean实现了DisposableBean这个接口,会调用那个其实现的destroy()方法;</p><p style="text-align: start;">10、最后,如果这个Bean的Spring配置中配置了destroy-method属性,会自动调用其配置的销毁方法。</p><p style="text-align: start;"><span style="color: rgb(255, 0, 0);"><strong>结合代码理解一下</strong></span><br></p><h3 style="text-align: start;"><strong>1、Bean的定义</strong></h3><p style="text-align: start;">Spring通常通过配置文件定义Bean。如:</p><pre><code class="language-java"><?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.0.xsd”> <bean id=”HelloWorld” class=”com.pqf.beans.HelloWorld”> <property name=”msg”> <value>HelloWorld</value> </property> </bean> </beans></code></pre><p><span style="color: rgb(62, 62, 62); background-color: rgb(255, 255, 255); font-size: 15px;">这个配置文件就定义了一个标识为 HelloWorld 的Bean。在一个配置文档中可以定义多个Bean。</span></p><h3 style="text-align: start;"><strong>2、Bean的初始化</strong></h3><p style="text-align: start;">有两种方式初始化Bean。</p><h4 style="text-align: start;"><span style="color: inherit;">1、在配置文档中通过指定init-method 属性来完成</span></h4><p style="text-align: start;">在Bean的类中实现一个初始化Bean属性的方法,如init(),如:</p><pre><code class="language-java">public class HelloWorld{ public String msg=null; public Date date=null; public void init() { msg=”HelloWorld”; date=new Date(); } …… }</code></pre><p style="text-align: start;">然后,在配置文件中设置init-mothod属性:</p><h4 style="text-align: start;"><span style="color: inherit;">2、实现 org.springframwork.beans.factory.InitializingBean接口</span></h4><p style="text-align: start;">Bean实现InitializingBean接口,并且增加 afterPropertiesSet() 方法:</p><pre><code class="language-java">public class HelloWorld implement InitializingBean { public String msg=null; public Date date=null; public void afterPropertiesSet() { msg="向全世界问好!"; date=new Date(); } …… }</code></pre><p style="text-align: start;">那么,当这个Bean的所有属性被Spring的BeanFactory设置完后,会自动调用afterPropertiesSet()方法对Bean进行初始化,于是,配置文件就不用指定 init-method属性了。</p><h3 style="text-align: start;"><span style="color: rgb(122, 68, 66);"><strong>3、Bean的调用</strong></span></h3><p style="text-align: start;">有三种方式可以得到Bean并进行调用:</p><h4 style="text-align: start;"><span style="color: inherit;">1、使用BeanWrapper</span></h4><pre><code class="language-java">HelloWorld hw=new HelloWorld(); BeanWrapper bw=new BeanWrapperImpl(hw); bw.setPropertyvalue(”msg”,”HelloWorld”); system.out.println(bw.getPropertyCalue(”msg”));</code></pre><h4 style="text-align: start;"><span style="color: inherit;">2、使用BeanFactory</span></h4><pre style="text-align: start;"><code class="language-java">InputStream is=new FileInputStream(”config.xml”); XmlBeanFactory factory=new XmlBeanFactory(is); HelloWorld hw=(HelloWorld) factory.getBean(”HelloWorld”); system.out.println(hw.getMsg());</code></pre><h4 style="text-align: start;"><span style="color: inherit;">3、使用ApplicationConttext</span></h4><pre style="text-align: start;"><code>ApplicationContext actx=new FleSystemXmlApplicationContext(”config.xml”); HelloWorld hw=(HelloWorld) actx.getBean(”HelloWorld”); System.out.println(hw.getMsg());</code></pre><h3 style="text-align: start;"><span style="color: rgb(122, 68, 66);"><strong>4、Bean的销毁</strong></span></h3><h4 style="text-align: start;"><span style="color: inherit;">1、使用配置文件中的 destory-method 属性</span></h4><p style="text-align: start;">与初始化属性 init-methods类似,在Bean的类中实现一个撤销Bean的方法,然后在配置文件中通过 destory-method指定,那么当bean销毁时,Spring将自动调用指定的销毁方法。</p><h4 style="text-align: start;"><span style="color: inherit;">2、实现 org.springframwork.bean.factory.DisposebleBean接口</span></h4><p style="text-align: start;">如果实现了DisposebleBean接口,那么Spring将自动调用bean中的Destory方法进行销毁,所以,Bean中必须提供Destory方法。</p><p style="text-align: justify;"><strong>图解</strong></p><p style="text-align: justify;"><img src="https://jsd.onmicrosoft.cn/gh/iGaoWei/codercdn@master/question/20240627/2024062710160675170.png" alt="https://jsd.onmicrosoft.cn/gh/iGaoWei/codercdn@master/question/20240627/2024062710160675170.png" data-href="" style=""></p>
<p style="text-align: justify;"><span style="color: rgb(255, 0, 0); font-size: 19px;"><strong>首先简单说一下(以下为一个回答的参考模板)</strong></span></p><p style="text-align: start;">1、实例化一个Bean--也就是我们常说的new;</p><p style="text-align: start;">2、按照Spring上下文对实例化的Bean进行配置--也就是IOC注入;</p><p style="text-align: start;">3、如果这个Bean已经实现了BeanNameAware接口,会调用它实现的setBeanName(String)方法,此处传递的就是Spring配置文件中Bean的id值</p><p style="text-align: start;">4、如果这个Bean已经实现了BeanFactoryAware接口,会调用它实现的setBeanFactory(setBeanFactory(BeanFactory)传递的是Spring工厂自身(可以用这个方式来获取其它Bean,只需在Spring配置文件中配置一个普通的Bean就可以);</p><p style="text-align: start;">5、如果这个Bean已经实现了ApplicationContextAware接口,会调用setApplicationContext(ApplicationContext)方法,传入Spring上下文(同样这个方式也可以实现步骤4的内容,但比4更好,因为ApplicationContext是BeanFactory的子接口,有更多的实现方法);</p><p style="text-align: start;">6、如果这个Bean关联了BeanPostProcessor接口,将会调用postProcessBeforeInitialization(Object obj, String s)方法,BeanPostProcessor经常被用作是Bean内容的更改,并且由于这个是在Bean初始化结束时调用那个的方法,也可以被应用于内存或缓存技术;</p><p style="text-align: start;">7、如果Bean在Spring配置文件中配置了init-method属性会自动调用其配置的初始化方法。</p><p style="text-align: start;">8、如果这个Bean关联了BeanPostProcessor接口,将会调用postProcessAfterInitialization(Object obj, String s)方法、;</p><blockquote style="text-align: start;">注:以上工作完成以后就可以应用这个Bean了,那这个Bean是一个Singleton的,所以一般情况下我们调用同一个id的Bean会是在内容地址相同的实例,当然在Spring配置文件中也可以配置非Singleton,这里我们不做赘述。</blockquote><p style="text-align: start;">9、当Bean不再需要时,会经过清理阶段,如果Bean实现了DisposableBean这个接口,会调用那个其实现的destroy()方法;</p><p style="text-align: start;">10、最后,如果这个Bean的Spring配置中配置了destroy-method属性,会自动调用其配置的销毁方法。</p><p style="text-align: start;"><span style="color: rgb(255, 0, 0);"><strong>结合代码理解一下</strong></span><br></p><h3 style="text-align: start;"><strong>1、Bean的定义</strong></h3><p style="text-align: start;">Spring通常通过配置文件定义Bean。如:</p><pre><code class="language-java"><?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.0.xsd”> <bean id=”HelloWorld” class=”com.pqf.beans.HelloWorld”> <property name=”msg”> <value>HelloWorld</value> </property> </bean> </beans></code></pre><p><span style="color: rgb(62, 62, 62); background-color: rgb(255, 255, 255); font-size: 15px;">这个配置文件就定义了一个标识为 HelloWorld 的Bean。在一个配置文档中可以定义多个Bean。</span></p><h3 style="text-align: start;"><strong>2、Bean的初始化</strong></h3><p style="text-align: start;">有两种方式初始化Bean。</p><h4 style="text-align: start;"><span style="color: inherit;">1、在配置文档中通过指定init-method 属性来完成</span></h4><p style="text-align: start;">在Bean的类中实现一个初始化Bean属性的方法,如init(),如:</p><pre><code class="language-java">public class HelloWorld{ public String msg=null; public Date date=null; public void init() { msg=”HelloWorld”; date=new Date(); } …… }</code></pre><p style="text-align: start;">然后,在配置文件中设置init-mothod属性:</p><h4 style="text-align: start;"><span style="color: inherit;">2、实现 org.springframwork.beans.factory.InitializingBean接口</span></h4><p style="text-align: start;">Bean实现InitializingBean接口,并且增加 afterPropertiesSet() 方法:</p><pre><code class="language-java">public class HelloWorld implement InitializingBean { public String msg=null; public Date date=null; public void afterPropertiesSet() { msg="向全世界问好!"; date=new Date(); } …… }</code></pre><p style="text-align: start;">那么,当这个Bean的所有属性被Spring的BeanFactory设置完后,会自动调用afterPropertiesSet()方法对Bean进行初始化,于是,配置文件就不用指定 init-method属性了。</p><h3 style="text-align: start;"><span style="color: rgb(122, 68, 66);"><strong>3、Bean的调用</strong></span></h3><p style="text-align: start;">有三种方式可以得到Bean并进行调用:</p><h4 style="text-align: start;"><span style="color: inherit;">1、使用BeanWrapper</span></h4><pre><code class="language-java">HelloWorld hw=new HelloWorld(); BeanWrapper bw=new BeanWrapperImpl(hw); bw.setPropertyvalue(”msg”,”HelloWorld”); system.out.println(bw.getPropertyCalue(”msg”));</code></pre><h4 style="text-align: start;"><span style="color: inherit;">2、使用BeanFactory</span></h4><pre style="text-align: start;"><code class="language-java">InputStream is=new FileInputStream(”config.xml”); XmlBeanFactory factory=new XmlBeanFactory(is); HelloWorld hw=(HelloWorld) factory.getBean(”HelloWorld”); system.out.println(hw.getMsg());</code></pre><h4 style="text-align: start;"><span style="color: inherit;">3、使用ApplicationConttext</span></h4><pre style="text-align: start;"><code>ApplicationContext actx=new FleSystemXmlApplicationContext(”config.xml”); HelloWorld hw=(HelloWorld) actx.getBean(”HelloWorld”); System.out.println(hw.getMsg());</code></pre><h3 style="text-align: start;"><span style="color: rgb(122, 68, 66);"><strong>4、Bean的销毁</strong></span></h3><h4 style="text-align: start;"><span style="color: inherit;">1、使用配置文件中的 destory-method 属性</span></h4><p style="text-align: start;">与初始化属性 init-methods类似,在Bean的类中实现一个撤销Bean的方法,然后在配置文件中通过 destory-method指定,那么当bean销毁时,Spring将自动调用指定的销毁方法。</p><h4 style="text-align: start;"><span style="color: inherit;">2、实现 org.springframwork.bean.factory.DisposebleBean接口</span></h4><p style="text-align: start;">如果实现了DisposebleBean接口,那么Spring将自动调用bean中的Destory方法进行销毁,所以,Bean中必须提供Destory方法。</p><p style="text-align: justify;"><strong>图解</strong></p><p style="text-align: justify;"><img src="https://jsd.onmicrosoft.cn/gh/iGaoWei/codercdn@master/question/20240627/2024062710160675170.png" alt="https://jsd.onmicrosoft.cn/gh/iGaoWei/codercdn@master/question/20240627/2024062710160675170.png" data-href="" style=""></p>
评论 (
0
)
登录
后才可以发表评论
状态
待办的
待办的
进行中
已完成
已关闭
负责人
未设置
标签
Java
未设置
标签管理
里程碑
未关联里程碑
未关联里程碑
Pull Requests
未关联
未关联
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
未关联
分支 (
-
)
标签 (
-
)
开始日期   -   截止日期
-
置顶选项
不置顶
置顶等级:高
置顶等级:中
置顶等级:低
优先级
不指定
严重
主要
次要
不重要
参与者(1)
1
https://gitee.com/DreamCoders/CoderGuide.git
git@gitee.com:DreamCoders/CoderGuide.git
DreamCoders
CoderGuide
CoderGuide
点此查找更多帮助
搜索帮助
Git 命令在线学习
如何在 Gitee 导入 GitHub 仓库
Git 仓库基础操作
企业版和社区版功能对比
SSH 公钥设置
如何处理代码冲突
仓库体积过大,如何减小?
如何找回被删除的仓库数据
Gitee 产品配额说明
GitHub仓库快速导入Gitee及同步更新
什么是 Release(发行版)
将 PHP 项目自动发布到 packagist.org
评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册