更多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