您的位置:首页 > 文旅 > 旅游 > 在线定制网站官网_番禺人才网_百度seo优化排名软件_网站外链工具

在线定制网站官网_番禺人才网_百度seo优化排名软件_网站外链工具

2025/4/13 11:30:46 来源:https://blog.csdn.net/m0_72167535/article/details/146216383  浏览:    关键词:在线定制网站官网_番禺人才网_百度seo优化排名软件_网站外链工具
在线定制网站官网_番禺人才网_百度seo优化排名软件_网站外链工具

目录

1.加载过程

2.示例:

3.注意:


1.加载过程

①Bean 实例化

通过构造函数或工厂方法创建 Bean 的实例

②手动设置属性

通过set方法进行赋值

③属性注入

Spring 容器将配置的属性值注入到 Bean 中(通过 @Autowired@Value@NacosValue 等注解或 XML 配置)。

④初始化

先调用调用 @PostConstruct 方法,再调用InitializingBeanafterPropertiesSet 方法,再调用自定义的初始化方法(通过 initMethod 指定)

⑤bean加载完成可以使用

⑥容器关闭时进行销毁

先调用 @PreDestroy 方法再调用 DisposableBeandestroy 方法。然后调用自定义的销毁方法(通过 destroyMethod 指定)

2.示例:

配置bean

package com.example.demo6.service;import lombok.Data;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;@Data
public class MyBean implements InitializingBean, DisposableBean {private Integer id;public MyBean() {System.out.println("执行无参构造1");}static {System.out.println("static1");}@Autowiredprivate MyBeans myBeans;@PostConstructpublic void postConstruct() {System.out.println("postConstruct1");}@Overridepublic void afterPropertiesSet() throws Exception {System.out.println("afterPropertiesSet1");}public void init() {System.out.println("init1");}@Overridepublic void destroy() throws Exception {System.out.println("afterPropertiesSet对应的销毁1");}@PreDestroypublic void preDestroy() {System.out.println("postConstruct对应的销毁1");}public void destroyMethod() {System.out.println("init对应的销毁1");}
}
package com.example.demo6.service;import lombok.Data;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.io.Serializable;@Data
public class MyBeans implements InitializingBean, DisposableBean {private Integer id;public MyBeans() {System.out.println("执行无参构造2");}static {System.out.println("static2");}@PostConstructpublic void postConstruct() {System.out.println("postConstruct2");}@Overridepublic void afterPropertiesSet() throws Exception {System.out.println("afterPropertiesSet2");}public void init() {System.out.println("init2");}@Overridepublic void destroy() throws Exception {System.out.println("afterPropertiesSet对应的销毁2");}@PreDestroypublic void preDestroy() {System.out.println("postConstruct对应的销毁2");}public void destroyMethod() {System.out.println("init对应的销毁2");}
}
package com.example.demo6.config;import com.example.demo6.service.MyBean;
import com.example.demo6.service.MyBeans;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class TestConfig {@Bean(initMethod="init",destroyMethod="destroyMethod")public MyBean myBean(){MyBean myBean = new MyBean();myBean.setId(1);System.out.println("属性赋值1");return myBean;}@Bean(initMethod="init",destroyMethod="destroyMethod")public MyBeans myBeans() {System.out.println("属性赋值2");return new MyBeans();}}

启动容器 

import com.example.demo6.config.TestConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class Test {public static void main(String[] args) {System.out.println("容器启动");AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfig.class);System.out.println("加载完成");System.out.println("bean使用");context.close();}
}

 执行结果:


容器启动
22:21:50.639 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@5474c6c
22:21:50.649 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
22:21:50.789 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
22:21:50.790 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
22:21:50.790 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
22:21:50.794 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
22:21:50.810 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'testConfig'
22:21:50.816 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'myBean'
static1
执行无参构造1
属性赋值1
22:21:50.839 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'myBeans'
static2
执行无参构造2
属性赋值2
postConstruct2
afterPropertiesSet2
init2
postConstruct1
afterPropertiesSet1
init1
加载完成
bean使用
22:21:50.859 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@5474c6c, started on Wed Mar 12 22:21:50 CST 2025
postConstruct对应的销毁1
afterPropertiesSet对应的销毁1
init对应的销毁1
postConstruct对应的销毁2
afterPropertiesSet对应的销毁2
init对应的销毁2

3.注意:

①静态代码块最先执行

②如果父类实现了InitializingBean接口,子类会执行父类的afterPropertiesSet方法,场景的动态数据源的实现类AbstractRoutingDataSource

③类的生命周期

  1. 加载(Loading)阶段:将类的字节码载入内存
  2. 链接(Linking)阶段
    1. 验证(Verification)确保 Class 文件的字节流符合 JVM 规范
    2. 准备(Preparation)为类的静态变量分配内存并设置初始值
    3. 解析(Resolution)将常量池中的符号引用替换为直接引用
  3. 初始化(Initialization)阶段
    1. 静态变量赋值
    2. 静态代码执行
  4. 使用(Using)阶段
    1. 类的实例化
    2. 属性注入
    3. 初始化方法执行
    4. 使用bean
  5. 卸载(Unloading)阶段

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com