Spring中bean的生命周期
来源:互联网 发布:网达软件 编辑:程序博客网 时间:2024/05/02 00:03
根据上面的图片流程进行代码验证了一下:applicationContext.xml(我取的另一个名字:beans.xml)内容:<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="personServer" init-method="myInit" destroy-method="myDestroy" scope="prototype" class="com.iflytek.person.PersonServer"> <property name="name" value="大锤" /> <property name="age" value="22"></property> </bean> <!--注入前置和后置处类--> <bean id="myBeanPostProcessor" class="com.iflytek.person.MyBeanPostProcessor"/> </beans>Server类:public class PersonServer implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean{ private String name; private int age; //业务逻辑方法 public void output() { System.out.println(name + "=================" + age); } //无参构造函数 public PersonServer() { System.out.println("PersonServer初始化"); } //有参构造函数 public PersonServer(int age, String name) { this.name = name; this.age = age; } public String getName() { return name; } //打印出set值 public void setName(String name) { System.out.println("name开始赋值:" + name); this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } //获取bean的id @Override public void setBeanName(String arg0) { System.out.println("bean 的 id 开始赋值:" + arg0); } //获取BeanFactory的对象 @Override public void setBeanFactory(BeanFactory arg0) throws BeansException { System.out.println("beanFactory 的id 开始赋值:" + arg0); } //获取容器对象 @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { System.err.println("applicationContext :" + applicationContext); } //在执行postProcessAfterInitialization的方法之前调用 @Override public void afterPropertiesSet() throws Exception { System.out.println("InitializingBean======================================="); } //在postProcessAfterInitialization和postProcessBeforeInitialization之间调用 public void myInit() { System.out.println("myInit()的方法"); } //自定义销毁方法 public void myDestroy() { System.out.println("销毁方法"); } }前置处理和后置处理类:public class MyBeanPostProcessor implements BeanPostProcessor{ @Override public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException { System.out.println("postProcessAfterInitialization:after"); return arg0; } @Override public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException { System.out.println("postProcessBeforeInitialization:before"); //System.out.println(arg0 + "时间:" + new Date() + " 类名:" + arg0.getClass().getName()); // TODO Auto-generated method stub return arg0; } }//Main方法public class TestApp{ /** * main:(这里用一句话描述这个方法的作用). <br/> * @author syzhao * @param args * @since JDK 1.6 */ public static void main(String[] args) { //BeanFactory context = new XmlBeanFactory(new ClassPathResource("com/iflytek/person/beans.xml")); ApplicationContext context = new ClassPathXmlApplicationContext("com/iflytek/person/beans.xml"); PersonServer person = (PersonServer)context.getBean("personServer"); person.output(); } }//打印出的结果Person初始化name开始赋值:大锤bean 的 id 开始赋值:personServerbeanFactory 的id 开始赋值:org.springframework.beans.factory.support.DefaultListableBeanFactory@b307f0: defining beans [personServer,myBeanPostProcessor]; root of factory hierarchyapplicationContext :org.springframework.context.support.ClassPathXmlApplicationContext@ece65: startup date [Sun Nov 01 20:58:56 CST 2015]; root of context hierarchypostProcessBeforeInitialization:beforeInitializingBean=======================================myInit()的方法postProcessAfterInitialization:after大锤=================22
0 0
- Spring中bean的生命周期
- Spring 中 Bean 的生命周期
- Spring 中bean 的生命周期
- Spring中Bean的生命周期
- Spring中bean的生命周期
- Spring中bean的生命周期
- Spring中bean的生命周期
- Spring中Bean的生命周期
- spring中bean的生命周期
- spring中bean的生命周期
- Spring中bean的生命周期
- spring中bean的生命周期
- Spring中Bean的生命周期
- Spring中bean的生命周期
- Spring中Bean的生命周期
- Spring中Bean的生命周期
- Spring中Bean的生命周期
- Spring中Bean的生命周期
- 黑马程序员——C语言指针
- linux常用命令ls
- 密码加盐-Salting password
- 闲聊
- JavaScript高级程序设计之DOM之节点层次之Text类型第10.1.4讲
- Spring中bean的生命周期
- Mangos源码分析(15):游戏对象的实现
- 《剑指offer》——顺时针打印矩阵
- 图像处理和图像识别中常用的matlab函数
- 括号匹配(二)
- 如何解决Android studio已分享到github的项目但是git->commit directory提交不上去
- Regular Expression实现
- V8内存管理之作用域
- hdoj--1495--非常可乐(搜索+隐式图)