欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring(三)

程序员文章站 2022-04-09 18:52:56
Spring bean的生命周期 1.需要的实体类 2.需要的InstantiationAwareBeanPostProcessorAdapter 3.需要的BeanPostProcessor 4.需要的BeanFactoryPostProcessor 5.需要的xml文件配置 6.需要的测试代码 ......

spring 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,就是为了执行初始化之后的操作 ,但是对sprin*生了依赖
 *         后续使用反射机制 init-method 来消除对spring的依赖
 * 04.实现了disposablebean,就是为了执行bean销毁之后的操作 ,但是对sprin*生了依赖
 *         后续使用反射机制 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();  //关闭容器
    }
}

   

    未完待续!!!