zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Spring框架:第三章:对象的生命周期及单例bean生命周期的11个步骤

Spring对象框架 步骤 11 生命周期 单例 bean
2023-06-13 09:11:59 时间

IOC之Bean的生命周期 实验22:创建带有生命周期方法的bean

public class Person { private Integer id; private String name;

public void init() {
    System.out.println("这是person对象的初始化方法");
}

public void destroy() {
    System.out.println("这是person对象的销毁方法");
}

配置信息:

测试代码:

@Test public void test13() throws Exception { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(“applicationContext.xml”); System.out.println(applicationContext.getBean(“p24”)); applicationContext.close(); }

Bean的后置处理器BeanPostProcessor bean的后置处理器,可以在bean对象初始化之前或之后做一些工作。 要使用bean的后置处理器,需要实现这个接口并配置。

实验23:测试bean的后置处理器

person对象,一定要有初始化方法

public class Person { private Integer id; private String name; private Car car;

public void init() {
    System.out.println("这是person对象的初始化方法");
}

更多内容请见原文,原文转载自:https://blog.csdn.net/weixin_44519496/article/details/120342971