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

【Java】Bean的生命周期——print大法带你了解Bean的生命周期(初探)

、定义一个Student类#

为了方便看到生命周期过程,我直接使用print大法;

另外,将类交给Spring容器管理可以通过xml文件形式和注解形式,接下来我都实现一下

1、xml形式#

package com.goahead.bean;

public class Student1 {

private String name;

public Student1() {

System.out.println("1、实例化");

}

public Student1(String name) {

this.name = name;

}

public void setName(String name) {

System.out.println("2、依赖注入");

this.name = name;

}

public void myInit() {

System.out.println("3、初始化");

}

public void myDestroy() {

System.out.println("5、销毁");

}

}

2、注解形式#

package com.goahead.bean;

import org.springframework.stereotype.Component;

import org.springframework.beans.factory.annotation.Value;

import javax.annotation.PostConstruct;

import javax.annotation.PreDestroy;

@Component

public class Student2 {

private String name;

public Student2() {

System.out.println("1、实例化");

}

public Student2(String name) {

this.name = name;

}

@Value("goahead")

public void setName(String name) {

System.out.println("2、依赖注入");

this.name = name;

}

@PostConstruct

public void myInit() {

System.out.println("3、初始化");

}

@PreDestroy

public void myDestroy() {

System.out.println("5、销毁");

}

}

二、将Student类交给Spring容器#

1、xml文件形式#

需要在resource文件夹下定义一个xml文件

<?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.xsd">

<bean id="student" class="com.goahead.bean.Student" init-method="myInit" destroy-method="myDestroy">

<property name="name" value="goahead"/>

</bean>

</beans>

2、注解形式(推荐)#

注解形式我们只需在Student2类上加相应的注解,然后加一个配置类

在config目录下创建一个 AppConfig类

package com.goahead.config;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

@Configuration

@ComponentScan("com.goahead.bean") // 扫描包路径

public class AppConfig {

}

三、测试#

由于我们第二步实现了两种方式,所以测试也对应两种方式

1、对应xml形式#

import com.goahead.bean.Student1;

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AppTest1 {

@Test

public void test1() {

ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");

Student1 student1 = context.getBean(Student1.class);

System.out.println("4、使用Bean");

((ClassPathXmlApplicationContext)context).close();

}

}

2、注解形式#

import com.goahead.bean.Student2;

import com.goahead.config.AppConfig;

import org.junit.Test;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class AppTest2 {

@Test

public void test2() {

// 使用AnnotationConfigApplicationContext替代ClassPathXmlApplicationContext

AnnotationConfigApplicationContext context =

new AnnotationConfigApplicationContext(AppConfig.class);

Student2 student = context.getBean(Student2.class);

System.out.println("4、使用Bean");

context.close(); // 触发销毁方法

}

}

四、测试结果#

1、xml形式#

image

2、注解形式#

image

http://www.cnnetsun.cn/news/40903.html

相关文章:

  • 汽车变速器电控系统Simulink模型:从原理到实现
  • Atmosphere自定义固件终极指南:从安装到故障排除
  • docker网络模式详解
  • 永磁同步电机基于非线性磁链观测器的转子位置估计策略:SCI一区顶刊复现与SIMULINK仿真
  • 异步电机直接转矩控制算法模型在R2016b版本及以上的正常运行
  • 从前端体验到后端架构:Airbnb全栈SDET面试深度解析
  • rtpengine作为媒体代理的一个问题
  • Caesium图像压缩器高级配置与定制化指南
  • Graphiti时序知识图谱:5大革新策略重塑动态知识管理
  • CMATH:如何5分钟掌握小学数学计算能力评估
  • 使用RNNoise进行音频降噪
  • 风储模型中的功率分配模型
  • 眼见非实(Bugku杂项入门)
  • 毕方Talon:鸿蒙开发的编译时安全守护神
  • 创客匠人峰会新解:高势能 IP 打造 ——AI 时代知识变现的十倍增长密码
  • Java线程池与Executor框架完全指南:一看就会,一看就懂!
  • 随机图床 _
  • PETools 逆向工程工具完整使用指南
  • 改善深层神经网络 第一周:深度学习的实践(四)其他缓解过拟合的方法
  • 品牌营销 深度心智方法论
  • STM32之使用DWT外设编写延时函数
  • 选择中国优化路线的美国独立服务器的8大好处
  • Abaqus三维纤维复合材料Vumat子程序:弹性层压板+Hashin损伤(纤维)+Puck损...
  • 移动应用无障碍测试完全指南:如何用Maestro实现WCAG标准自动化验证
  • Effective C++ 中文版第3版:C++进阶编程的终极指南
  • Slang光线追踪加速结构深度解析:从理论到性能优化实践
  • DeepSeek-R1-Distill-Qwen-7B集群部署终极指南:轻松搞定AI推理服务
  • 风储调频在Matlab/Simulink中的探索:基于四机两区系统的实践
  • 基于Java Swing的猜数字小游戏(2)
  • 提升 Web 端 JavaScript 的可信度:WAICT 体系详解