Spring1.2——基于xml的bean装配的生命周期

更多JAVA框架学习视频点这里
初始化和销毁:
1,需要有初始化和销毁方法,然后在xml中对应进行配置
2,初始化会自动调用,销毁需要手动调用
3,被销毁容器得是close方法并且只能销毁单例
例子

package com.d_lifestyle;

public class Car {
    public void run() {
        System.out.println("car run");
    }
    public void carInit() {
        System.out.println("car init");
    }
    public void carDestroy() {
        System.out.println("car destroy");
    }
}

配置文件

    <bean id="Car" class="com.d_lifestyle.Car" init-method="carInit" destroy-method="carDestroy"></bean>

测试类

    @Test
    public void test1() throws Exception {
        String xmlPath = "com/d_lifestyle/beans.xml";
/*      ApplicationContext appct = new ClassPathXmlApplicationContext(xmlPath);
        Car car = appct.getBean("Car", Car.class);
        appct.getClass().getMethod("close").invoke(appct);*/

        ClassPathXmlApplicationContext appct = new ClassPathXmlApplicationContext(xmlPath);
        Car car = appct.getBean("Car", Car.class);
        appct.close();
    }

结果

car init
七月 27, 2018 9:30:43 下午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@192d3247: startup date [Fri Jul 27 21:30:43 CST 2018]; root of context hierarchy
七月 27, 2018 9:30:43 下午 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
信息: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@10db82ae: defining beans [Car]; root of factory hierarchy
car destroy

BeanPostProcessor后处理bean:
后处理类:

package com.d_lifestyle2;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

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

public class MyBeanPostProcessor implements BeanPostProcessor {

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

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

配置文件:

    <bean id="Car" class="com.d_lifestyle2.Car" init-method="carInit" destroy-method="carDestroy"></bean>
    <!-- 将后处理类提供给spring -->
    <bean class="com.d_lifestyle2.MyBeanPostProcessor" ></bean>

测试类:

    @Test
    public void test1() throws Exception {
        String xmlPath = "com/d_lifestyle2/beans.xml";
        ClassPathXmlApplicationContext appct = new ClassPathXmlApplicationContext(xmlPath);
        Car car = appct.getBean("Car", Car.class);
        appct.close();
    }

结果:

BeforeInit
car init
AfterInit
七月 29, 2018 7:09:28 下午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@1d371b2d: startup date [Sun Jul 29 19:09:28 CST 2018]; root of context hierarchy
七月 29, 2018 7:09:28 下午 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
信息: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2133814f: defining beans [Car,com.d_lifestyle2.MyBeanPostProcessor#0]; root of factory hierarchy
car destroy
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值