Spring-管理Bean的生命周期

注意:使用构造器创建Bean实例的时候,生成一个实例,而在生成时就已经调用了构造方法去实例化而得到这个实 例。之后再把这个实例赋给我们所要创建的Bean,从而在SpringIOC容器中管理这个Bean的生命周期。

下面以一个例子去说明

beans-cycle.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/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="car" class="cycle.Car" init-method="init" destroy-method="destroy">
        <property name="brand" value="baoma"></property>
        <property name="price" value="10000.00"></property>
        <property name="tyrePerimeter" value="#{T(java.lang.Math).PI * 80}"></property>
    </bean>


</beans>

 

Car类文件代码: 

package cycle;

public class Car {
    public String brand;
    public double price;
    public double tyrePerimeter;

    public Car(){
        System.out.println ("construt......");
    }
    public void init(){
        System.out.println ("init.....");
    }
    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        System.out.println ("setBrand");
        this.brand = brand;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        System.out.println ("setPrice");
        this.price = price;
    }

    public double getTyrePerimeter() {
        return tyrePerimeter;
    }

    public void setTyrePerimeter(double tyrePerimeter) {
        System.out.println ("setTyrePerimeter");
        this.tyrePerimeter = tyrePerimeter;
    }

    @Override
    public String toString() {
        return "Car{" +
                "brand='" + brand + '\'' +
                ", price=" + price +
                ", tyrePerimeter=" + tyrePerimeter +
                '}';
    }
    public void destroy(){
        System.out.println ("destroy....");
    }
}

Main类的代码: 

package cycle;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String args[]){
        ClassPathXmlApplicationContext clt = new ClassPathXmlApplicationContext ("beans-cycle.xml");
        System.out.println (clt);
//        关闭IOC容器
        clt.close ();

    }
}

 

                 创  建 Bean 后 置 处 理 器 

 

package cycle;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyBeanProcess implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
         System.out.println("postProcessBeforeInitialization....." + bean + beanName);
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
         System.out.println("postProcessAfterInitialization....." + bean + beanName);
        Car car = new Car ();
        car.setBrand ("aodi");
        return car;





    }
}

 

 

列一个例子来说明

配置文件的代码

<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="car" class="cycle.Car" init-method="Init" destroy-method="destroy">
        <property name="brand" value="chen"></property>
        <property name="price" value="100000.00"></property>
        <property name="tyclePerimeter" value="#{T(java.lang.Math).PI * 80}"></property>
    </bean>
    <!--配置Bean的后置处理器-->
    <bean class="cycle.MyBeanProcess"></bean>
</beans>

 

后置处理器MyBeanProcess 这个类的代码

package cycle;

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

public class MyBeanProcess implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println ("postProcessBeforeInitialization" + bean + beanName);
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println ("postProcessAfterInitialization" + bean + beanName);
        return bean;
    }
}

 

Car类的代码

package cycle;

public class Car {
    public String brand;
    public double price;
    public String tyclePerimeter;

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        System.out.println ("brand....");
        this.brand = brand;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        System.out.println ("price.....");
        this.price = price;
    }

    public String getTyclePerimeter() {
        return tyclePerimeter;
    }

    public void setTyclePerimeter(String tyclePerimeter) {
        System.out.println ("tyclePerimeter....");
        this.tyclePerimeter = tyclePerimeter;
    }
    public Car(){
        System.out.println ("construt.......");
    }
    public void Init(){
        System.out.println ("init......");
    }
    public void destroy(){
        System.out.println ("destroy......");
    }
    @Override
    public String toString() {
        return "Car{" +
                "brand='" + brand + '\'' +
                ", price=" + price +
                ", tyclePerimeter='" + tyclePerimeter + '\'' +
                '}';
    }
}

 

Main类的代码

package cycle;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String args[]){
        ClassPathXmlApplicationContext clt = new ClassPathXmlApplicationContext ("beans-cycle.xml");
        System.out.println (clt);
//        关闭IOC容器
        clt.close ();

    }
}

  

输出的结果

setBrand
setPrice
postProcessBeforeInitializationCar{brand='baoma', price=1000.0, circumference=157.07963267948966}car
init.....
postProcessAfterInitializationCar{brand='baoma', price=1000.0, circumference=157.07963267948966}car
Car{brand='baoma', price=1000.0, circumference=157.07963267948966}

Process finished with exit code 0

 

在后置处理器的 postProcessAfterInitialization()方法中去更改car对象的属性

MyBeanProcess.java 文件如下,其它文件与上面一样:

package cycle;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyBeanProcess implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessBeforeInitialization.....");
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessAfterInitialization.....");
        Car car = new Car ();//先定义一个car类的对象
        car.setBrand ("aodi");//再更改了car对象的属性
        return car; //返回car对象





    }
}

 

输出结果:

setBrand
setPrice
postProcessBeforeInitialization.....
init.....
postProcessAfterInitialization.....
setBrand
Car{brand='aodi', price=0.0, circumference=0.0}

Process finished with exit code 0

 

说明:之前car对象是baoma,而之后使用后置处理器的 postProcessAfterInitialization()方法中去更改car对象的brand属性为aodi。而改了Car的属性,Spring容器中自动更新bean,就更改了池中的bean。但是输出bean对象时,依然是之前那个被xml配置文件中的反射方式实例化的bean。所以输出的bean的属性,依然是以前的,没有更改过的。因为不管如何更改池中的bean,在输出bean的时候,都不会影响。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值