Spring Bean的生命周期

 面试的时候问到了这个问题,很长时间没接触spring了,回答的不好,

今天总结下:

实例化bean对象(通过构造方法或者工厂方法)
设置对象属性(setter等)(依赖注入)

如果Bean实现了BeanNameAware接口,工厂调用Bean的setBeanName()方法传递Bean的ID。(和下面的一条均属于检查Aware接口)
如果Bean实现了BeanFactoryAware接口,工厂调用setBeanFactory()方法传入工厂自身

调用Bean的初始化方法
使用Bean
容器关闭之前,调用Bean的销毁方法

spring的配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p" 
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee"
        xsi:schemaLocation="
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">
            
            
        <bean id="user" class="org.pine.spring.dto.User" init-method="initUser" destroy-method="destoryUser">
           <property name="fullName" value="张泰松"/>
        </bean>    
            
</beans>

实体类User:

 1 package org.pine.spring.dto;
 2 
 3 import org.springframework.beans.BeansException;
 4 import org.springframework.beans.factory.BeanFactory;
 5 import org.springframework.beans.factory.BeanFactoryAware;
 6 import org.springframework.beans.factory.BeanNameAware;
 7 
 8 public class User implements BeanNameAware,BeanFactoryAware{
 9 
10     private String fullName;
11 
12     public User() {
13         System.out.println("调用构造方法进行实例化~~~");
14     }
15 
16     public String getFullName() {
17         return fullName;
18     }
19 
20     public void setFullName(String fullName) {
21         System.out.println("调用set方法设置属性值~~~");
22         this.fullName = fullName;
23     }
24 
25     @Override
26     public String toString() {
27         return "User [fullName=" + fullName + "]";
28     }
29 
30     @Override
31     public void setBeanName(String beanName) {
32         System.out.println("设置beanName:"+beanName);
33     }
34     
35     @Override
36     public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
37        System.out.println("设置beanFactory:"+beanFactory);         
38     }
39 
40     public void initUser() {
41         System.out.println("初始化方法~~~");
42     }
43     
44     public void doSomething() {
45         System.out.println("做一些事情~~~");
46     }
47     
48     public void destoryUser() {
49         System.out.println("销毁方法~~~");
50     }
51     
52 }

测试类:

 1 package org.pine.spring.test;
 2 
 3 import org.pine.spring.dto.User;
 4 import org.springframework.context.ApplicationContext;
 5 import org.springframework.context.support.AbstractApplicationContext;
 6 import org.springframework.context.support.ClassPathXmlApplicationContext;
 7 
 8 public class SpringTest {
 9     public static void main(String[] args) {
10         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
11         User user=    (User) context.getBean("user");
12         user.doSomething();
13         ((AbstractApplicationContext)context).close();
14     }
15 }

测试结果如下:

调用构造方法进行实例化~~~
调用set方法设置属性值~~~
设置beanName:user
设置beanFactory:org.springframework.beans.factory.support.DefaultListableBeanFactory@449b2d27: defining beans [user]; root of factory hierarchy
初始化方法~~~
做一些事情~~~
销毁方法~~~

本文源代码在此处下载:SpringTest.zip

posted @ 2019-09-24 09:17  松松敲代码  阅读(...)  评论(...编辑  收藏