1.以ApplocationContext上下文单例模式装配bean为例,深入探讨bean的生命周期:
创新互联专注为客户提供全方位的互联网综合服务,包含不限于网站设计、网站制作、普陀网络推广、微信小程序定制开发、普陀网络营销、普陀企业策划、普陀品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供普陀建站搭建服务,24小时服务热线:028-86922220,官方网址:www.cdcxhl.com
(1).生命周期图:
(2).具体事例:
person类实现BeanNameAware,BeanFactoryAware接口
public class Person implements BeanNameAware ,BeanFactoryAware{ private String name; public 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配置文件:
<?xml version="1.0" encoding="UTF-8"?>
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).生命周期图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。