当前位置: 首页 > wzjs >正文

前端培训班深圳百度搜索排名优化

前端培训班,深圳百度搜索排名优化,建设销售型企业网站,抽奖怎么做网站目录 1.加载过程 2.示例: 3.注意: 1.加载过程 ①Bean 实例化 通过构造函数或工厂方法创建 Bean 的实例 ②手动设置属性 通过set方法进行赋值 ③属性注入 Spring 容器将配置的属性值注入到 Bean 中(通过 Autowired、Value、NacosValue…

目录

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)阶段
http://www.dtcms.com/wzjs/316032.html

相关文章:

  • 网页设计版权怎么写知乎关键词排名优化工具
  • 武汉网站建设设计哪家好购物网站页面设计
  • 网站专项审批查询沈阳网站优化
  • 网站模板是什么意思百度关键词排名十大排名
  • 网站建站建设工作总结百搜科技
  • 怎么在av网站做引流歌尔股份砍单
  • 宜兴建设局质监网站培训班招生方案
  • 做期货看啥子网站网页模板代码
  • 福建凭祥建设工程有限公司网站黄冈网站推广优化找哪家
  • 三门峡网站建设费用seo视频教程百度网盘
  • 移动互联网开发软件设计优化公司网站
  • 小型网站开发时间周期搜索引擎优化网站
  • html特效网站网站关键词优化排名外包
  • 人才网站建设cms精准引流获客软件
  • 58南浔做网站深圳优化服务
  • 做网站主页效果图北京seo相关
  • 南京网站建设润洽完整的网页设计代码
  • 樟木头镇网站仿做备案域名购买
  • 网站管理人员队伍建设有待加强沈阳seo按天计费
  • 创建网站代码推推蛙seo
  • 云南城乡建设厅网站网站推广工具有哪些
  • 北京市工程建设信息交易网站怎么弄一个自己的链接
  • 香港公司能在国内做网站网络营销首先要做什么
  • 新建的网站百度搜索不到营销推广的特点是
  • 中国建设监理协会网站继续教育电影站的seo
  • 国家电网账号注册网站帐号是什么关键词举例
  • 步步高网站建设报告网络推广软文
  • 企业网站四种类型东莞最新消息今天
  • 许昌 网站开发做网上推广
  • 自建房设计软件东莞关键字排名优化