创建一个Animal的类实现InitializingBean和DisposableBean接口,代码如下:
package io.mykit.spring.plugins.register.bean;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
/**
-
@author binghe
-
@version 1.0.0
-
@description 测试InitializingBean接口和DisposableBean接口
*/
public class Animal implements InitializingBean, DisposableBean {
public Animal(){
System.out.println(“执行了Animal类的无参数构造方法”);
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println(“执行了Animal类的初始化方法。。。。。”);
}
@Override
public void destroy() throws Exception {
System.out.println(“执行了Animal类的销毁方法。。。。。”);
}
}
接下来,我们新建一个AnimalConfig类,并将Animal通过@Bean注解的方式注册到Spring容器中,如下所示。
package io.mykit.spring.plugins.register.config;
import io.mykit.spring.plugins.register.bean.Animal;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
-
@author binghe
-
@version 1.0.0
-
@description AnimalConfig
*/
@Configuration
@ComponentScan(“io.mykit.spring.plugins.register.bean”)
public class AnimalConfig {
@Bean
public Animal animal(){
return new Animal();
}
}
接下来,我们在BeanLifeCircleTest类中新增testBeanLifeCircle02()方法来进行测试,如下所示。
@Test
public void testBeanLifeCircle02(){
//创建IOC容器
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AnimalConfig.class);
System.out.println(“IOC容器创建完成…”);
//关闭IOC容器
context.close();
}
运行BeanLifeCircleTest类中的testBeanLifeCircle02()方法,输出的结果信息如下所示。
执行了Animal类的无参数构造方法
执行了Animal类的初始化方法。。。。。
IOC容器创建完成…
执行了Animal类的销毁方法。。。。。
从输出的结果信息可以看出:单实例bean下,IOC容器创建完成后,会自动调用bean的初始化方法;而在容器销毁前,会自动调用bean的销毁方法。
多实例bean的案例代码基本与单实例bean的案例代码相同,只不过在AnimalConfig类中,我们在animal()方法上添加了@Scope(“prototype”)注解,如下所示。
package io.mykit.spring.plugins.register.config;
import io.mykit.spring.plugins.register.bean.Animal;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
/**
-
@author binghe
-
@version 1.0.0
-
@description AnimalConfig
*/
@Configuration
@ComponentScan(“io.mykit.spring.plugins.register.bean”)
public class AnimalConfig {
@Bean
@Scope(“prototype”)
public Animal animal(){
return new Animal();
}
}
接下来,我们在BeanLifeCircleTest类中新增testBeanLifeCircle03()方法来进行测试,如下所示。
@Test
public void testBeanLifeCircle03(){
//创建IOC容器
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AnimalConfig.class);
System.out.println(“IOC容器创建完成…”);
System.out.println(“-------”);
//调用时创建对象
Object bean = ctx.getBean(“animal”);
System.out.println(“-------”);
//调用时创建对象
Object bean1 = ctx.getBean(“animal”);
System.out.println(“-------”);
//关闭IOC容器
ctx.close();
}
先自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《Java开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以扫码领取!
![img](https://img-blog.csdnimg.cn/img_convert/1bf2de7ac93e58d4248f083e4feb5882.jpeg)
最后
经过日积月累, 以下是小编归纳整理的深入了解Java虚拟机文档,希望可以帮助大家过关斩将顺利通过面试。
由于整个文档比较全面,内容比较多,篇幅不允许,下面以截图方式展示 。
由于篇幅限制,文档的详解资料太全面,细节内容太多,所以只把部分知识点截图出来粗略的介绍,每个小节点里面都有更细化的内容!
-1711477949277)]
[外链图片转存中…(img-6UZS2Kjt-1711477949278)]
[外链图片转存中…(img-jts9w1zg-1711477949278)]
[外链图片转存中…(img-apRqOCFZ-1711477949278)]
[外链图片转存中…(img-riO0aJgQ-1711477949278)]
[外链图片转存中…(img-JLG8i4JH-1711477949278)]
由于篇幅限制,文档的详解资料太全面,细节内容太多,所以只把部分知识点截图出来粗略的介绍,每个小节点里面都有更细化的内容!
需要更多Java资料的小伙伴可以帮忙点赞+关注,点击传送门,即可免费领取!