Spring(三)--Spring bean的生命周期

2018-09-18 06:33:33来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

Spring bean的生命周期

 

          ApplicationContext Bean生命周期流程

 

 

1.需要的实体类

 

ackage com.xdf.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;

/**
 * 学生的实体类
 *
 * Aware本意就是察觉,感觉
 * 01.实现了BeanNameAware,就是让student类感觉到自己在容器中的id或者是name
 * 02.实现了BeanFactoryAware,就是让student类感觉到自己在容器中所属的bean工厂
 * 03.实现了InitializingBean,就是为了执行初始化之后的操作 ,但是对spring产生了依赖
 *         后续使用反射机制 init-method 来消除对spring的依赖
 * 04.实现了DisposableBean,就是为了执行bean销毁之后的操作 ,但是对spring产生了依赖
 *         后续使用反射机制 destroy-method 来消除对spring的依赖
 */
public class Student implements BeanNameAware,BeanFactoryAware,InitializingBean,DisposableBean{
    private  int  age;  //年龄
    private  String  stuName; //姓名

    private  String  beanName; //bean在容器中的id或者name
    private BeanFactory beanFactory; //bean所在的工厂

    public Student() {
        System.out.println("===Student类中的无参构造===");
    }

    //BeanNameAware接口中的setBeanName()
    public void setBeanName(String beanName) {
        System.out.println("===执行了BeanNameAware接口中的setBeanName()===");
        this.beanName=beanName;
    }

    //BeanFactoryAware中的setBeanFactory()
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("===执行了BeanFactoryAware中的setBeanFactory()===");
        this.beanFactory=beanFactory;
    }


    public void initMethod(){
        System.out.println("===Student类中的initMethod()===");
    }

    public void afterPropertiesSet() throws Exception {
        System.out.println("===InitializingBean中的afterPropertiesSet()===");
    }

    public  void myDestroy(){
        System.out.println("===Student类中的myDestroy()===");
    }

    public void destroy() throws Exception {
        System.out.println("===DisposableBean中的destroy()===");
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        System.out.println("===Student类中给属性赋值 setAge()===");
        this.age = age;
    }

    public String getStuName() {
        return stuName;
    }

    public void setStuName(String stuName) {
        System.out.println("===Student类中给属性赋值 setStuName()===");
        this.stuName = stuName;
    }

    public String getBeanName() {
        return beanName;
    }

    public BeanFactory getBeanFactory() {
        return beanFactory;
    }

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", stuName='" + stuName + '\'' +
                '}';
    }

2.需要的InstantiationAwareBeanPostProcessorAdapter

 

package com.xdf.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;

import java.beans.PropertyDescriptor;

/**
 * bean实例化之前 和之后
 */
public class MyInitAwareBeanpostAdpater  extends InstantiationAwareBeanPostProcessorAdapter{

    public  MyInitAwareBeanpostAdpater(){
        System.out.println("*****MyInitAwareBeanpostAdpater的无参构造*****");
    }


    //在实例化bean之前调用
    @Override
    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
        System.out.println("*****执行了MyInitAwareBeanpostAdpater的 postProcessBeforeInstantiation *****");
        return null; //底层返回的就是null
    }


    //在实例化bean之后调用
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("*****执行了MyInitAwareBeanpostAdpater的 postProcessAfterInitialization *****");
        return bean;
    }
    @Override
    public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
        System.out.println("*****执行了MyInitAwareBeanpostAdpater的 postProcessPropertyValues *****");
        return pvs;
    }

}

 

3.需要的BeanPostProcessor

 

package com.xdf.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * Processor  本意是加工 处理的意思!
 *
 * 实现了BeanPostProcessor
 */
public class MyBeanPostProcessor implements BeanPostProcessor {
    public MyBeanPostProcessor(){
        System.out.println("===MyBeanPostProcessor的无参构造方法 ===");
    }

    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("===执行了BeanPostProcessor中的 postProcess ==Before==Initialization  ===");
        return bean;
    }

    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("===执行了BeanPostProcessor中的 postProcess ==After==Initialization  ===");
        return bean;
    }
}

 

4.需要的BeanFactoryPostProcessor

 

package com.xdf.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

/**
 * Processor  本意是加工 处理的意思!
 *
 * 实现了BeanFactoryPostProcessor   工厂的后处理器
 */
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
    public MyBeanFactoryPostProcessor(){
        System.out.println("===MyBeanFactoryPostProcessor的无参构造方法 ===");
    }


    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        System.out.println("===MyBeanFactoryPostProcessor的postProcessBeanFactory ===");
        BeanDefinition beanDefinition = beanFactory.getBeanDefinition("student");
        beanDefinition.getPropertyValues().addPropertyValue("stuName","小白");
    }
}

 

5.需要的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"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--配置MyBeanPostprocessor   容器级别的 当前xml文件中所有的bean都会执行MyBeanPostProcessor中的方法-->
    <bean class="com.xdf.bean.MyBeanPostProcessor"/>
    <!--配置MyBeanFactoryPostprocessor 容器级别的  同上-->
    <bean class="com.xdf.bean.MyBeanFactoryPostProcessor"/>
    <!--配置MyInitAwareBeanpostAdpater  容器级别的  同上-->
    <bean class="com.xdf.bean.MyInitAwareBeanpostAdpater"/>



    <!-- 配置Student 的实体对象-->
    <bean id="student" class="com.xdf.bean.Student" init-method="initMethod" destroy-method="myDestroy">
         <property name="age" value="20"/>
         <property name="stuName" value="小黑"/>
    </bean>
</beans>

 

6.需要的测试代码

 

/**
 * 测试bean生命周期
 */
public class LifeCycle {

    public static void main(String[] args) {
        ApplicationContext  context=new ClassPathXmlApplicationContext("spring.xml");
        Student student= context.getBean("student", Student.class);
        System.out.println(student);
        ((ClassPathXmlApplicationContext)context).close();  //关闭容器
    }
}

   

    未完待续!!!

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Java基础——JDBC

下一篇:java中Class.getMethods()和Class.getDeclaredMethods()的区别