深入理解Spring中bean的生命周期介绍

网友投稿 185 2023-05-30


深入理解Spring中bean的生命周期介绍

1.以ApplocationContext上下文单例模式装配bean为例,深入探讨bean的生命周期:

(1).生命周期图:

(2).具体事例:

pePSXSpGrson类实现BeanNameAware,BeanFactoryAware接口

public class Person implements BeanNameAware ,BeanFactoryAware{

private String name;

publiPSXSpGc Person(){

System.out.println("调用构造器为属性值初始化");

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

@Override

public void setBeanName(String arg0) {

// TODO Auto-generated method stub

System.out.println("获取beanName id值"+" "+arg0);

}

@Override

public void setBeanFactory(BeanFactory arg0) throws BeansException {

// TODO Auto-generated method stub

System.out.println("获取BeanFactory" +" "+arg0);

}

}

public class MyBeanPostProcessor implements BeanPostProcessor{

@Override

public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException {

// TODO Auto-generated method stub

System.out.println("调用postProcessAfterInitialization");

return arg0;

}

@Override

public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException {

// TODO Auto-generated method stub

System.out.println("调用postProcessBeforeInitialization");

return arg0;

}

}

ApplicationContext.xml配置文件:

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd">

Main.java

public class Main {

public static void main(String[] args) {

// 创建IOC容器

ApplicationContext ac = new ClassPathXmlApplicationContext("org/jingdong/bean/life/applicationContext.xml");

//从容器中获取bean实例

Person person = (Person) ac.getBean("person");

//使用bean

System.out.println(person.getName());

}

}

2.以Spring Factory装配bean为例:

(1).生命周期图:


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:struts2实现文件下载功能
下一篇:spring boot 配置Filter过滤器的方法
相关文章

 发表评论

暂时没有评论,来抢沙发吧~