原文链接:https://juejin.cn/post/7105038058675765262
常规的 Spring IoC 容器中Bean的作用域有两种:singleton(单例)和prototype(非单例)
注:基于Web的容器还有其他种作用域,在这就不赘述了。
public class Dept {
//部门编号
private int deptNo;
//部门名称
private String deptName;
}
复制代码
复制代码
public static void main(String[] args) {
//获取IoC容器
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//从容器中获取对象
Dept dept1 = context.getBean("dept", Dept.class);
Dept dept2 = context.getBean("dept", Dept.class);
//打印对象
System.out.println(dept1);
System.out.println(dept2);
}
复制代码
复制代码
spring bean默认为单例,避免了对象的频繁创建与销毁,达到了bean对象的复用,性能高。
像表现层、业务层、数据层、工具类对象只需要调用方法,比较适合交给Spring IoC容器管理
但是像那种需要封装实例的域对象,因为会引发线程安全问题,不适合交给Spring IoC容器管理。
Spring Bean生命周期:Spring Bean 对象从创建到销毁的整体过程。
Spring Bean生命周期大致可以分为以下 5 个阶段:1.Bean 的实例化、2.Bean 属性赋值、3.Bean 的初始化、4.Bean 的使用、5.Bean 的销毁
Spring 根据 Bean 的作用域来选择 Bean 的管理方式。
综上所述: 为了更好研究如何控制bean周期,下面案例中创建的bean默认都使用单例模式。
由于ApplicationContext类中没有关闭容器的方法,所以想要关闭容器需要用到ApplicationContext的子类——ClassPathXmlApplicationContext类。该类又有两种方法可以关闭容器
//获取 ClassPathXmlApplicationContext 容器
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//调用close方法关闭容器
context.close();
复制代码
//获取 ClassPathXmlApplicationContext 容器
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//调用注册狗子关闭容器
context.registerShutdownHook();
复制代码
Bean 生命周期回调的方法主要有两种:
我们可以通过以下方式 2种方式自定义 Bean 的生命周期回调方法:
我们可以在 Spring Bean 的 Java 类中,通过实现 InitializingBean 和 DisposableBean 接口,指定 Bean 的生命周期回调方法。
/**
* 继承接口,程序初始化回调和销毁回调方法
*/
public class User implements InitializingBean, DisposableBean {
String name;
int age;
//setter方法
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
//初始化回调方法
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("这是初始化回调方法");
}
//销毁回调方法
@Override
public void destroy() throws Exception {
System.out.println("这是销毁回调方法");
}
//toString方法
@Override
public String toString() {
return "User{" +
"name='" + name + ''' +
", age=" + age +
'}';
}
}
复制代码
复制代码
public class App {
public static void main(String[] args) {
//获取 ClassPathXmlApplicationContext 容器
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//从容器中获取对象
User user = context.getBean("user", User.class);
//使用bean
System.out.println(user);
//调用close方法关闭容器
context.close();
}
}
复制代码
注意:由于通过接口设置生命周期的方式会导致代码的耦合性过高,所以通常情况下,我们会通过xml设置生命周期。
通过
/**
* 通过XML配置指定回调方法
*/
public class User {
String name;
int age;
//setter方法
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
//初始化回调方法
public void init() throws Exception {
System.out.println("这是初始化回调方法");
}
//销毁回调方法
public void destroy() throws Exception {
System.out.println("这是销毁回调方法");
}
//toString方法
@Override
public String toString() {
return "User{" +
"name='" + name + ''' +
", age=" + age +
'}';
}
}
复制代码
复制代码
页面更新:2024-03-19
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号