spring6里程碑版本的仓库
依赖:spring context依赖、junit依赖、log4j2依赖
log4j2.xml文件放到类路径下。
Spring其实就是一个管理Bean对象的工厂。它负责对象的创建,对象的销毁等。
所谓的生命周期就是:对象从创建开始到最终销毁的整个过程。
Bean生命周期的管理,可以参考Spring的源码:AbstractAutowireCapableBeanFactory类的doCreateBean()方法。
其实生命周期的本质是:在哪个时间节点上调用了哪个类的哪个方法。
我们需要充分的了解在这个生命线上,都有哪些特殊的时间节点。
只有我们知道了特殊的时间节点都在哪,到时我们才可以确定代码写到哪。
我们可能需要在某个特殊的时间点上执行一段特定的代码,这段代码就可以放到这个节点上。当生命线走到这里的时候,自然会被调用。
Bean生命周期之五大步:
定义一个Bean:SpringBean5类
初始化方法和销毁方法需要自己定义。
public class SpringBean5 {private String name;public void setName(String name) {System.out.println("第二步:Bean属性赋值。");this.name = name;}public SpringBean5() {System.out.println("第一步:实例化Bean。");}public void init(){//这个方法需要自己写自己配,不一定是这个方法名,方法名随意。System.out.println("第三步:初始化Bean。");}public void destroy(){//这个方法也需要自己写自己配,不一定是这个方法名,方法名随意。System.out.println("第五步:销毁Bean。");}}
创建spring5.xml配置:
需要在配置文件配置通过init-method属性配置初始化方法,通过destroy-method属性配置销毁方法
测试程序:
@Testpublic void testBeanLifecycle5(){ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring5.xml");SpringBean5 springBean5 = applicationContext.getBean("springBean5", SpringBean5.class);System.out.println("第四步:使用Bean:" + springBean5);//必须手动关闭Spring容器,注意Spring容器才会销毁Bean//applicationContext没有close这个方法,这个方法在ClassPathXmlApplicationContext里面,需要进行转型((ClassPathXmlApplicationContext) applicationContext).close();}
Bean生命周期之七大步:比五步在初始化Bean的前和后多两步
定义一个Bean:SpringBean7类
public class SpringBean7 {private String name;public void setName(String name) {System.out.println("第二步:Bean属性赋值。");this.name = name;}public SpringBean7() {System.out.println("第一步:实例化Bean。");}public void init(){//这个方法需要自己写自己配,不一定是这个方法名,方法名随意。System.out.println("第四步:初始化Bean。");}public void destroy(){//这个方法也需要自己写自己配,不一定是这个方法名,方法名随意。System.out.println("第七步:销毁Bean。");}}
要想实现“Bean后处理器”,需要实现BeanPostProcessor接口,并且重写before和after方法
接口如下:
这两个方法都是默认方法,都有两个参数,并且默认都返回bean
public interface BeanPostProcessor {@Nullabledefault Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {return bean;}@Nullabledefault Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {return bean;}
}
创建一个SpringBeanPostProcessor类实现该接口:
public class SpringBeanPostProcessor implements BeanPostProcessor {@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {System.out.println("第三步:执行Bean后处理器的before方法。");return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);}@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {System.out.println("第五步:执行Bean后处理器的after方法。");return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);}
}
创建spring7.xml配置:
需要配置Bean后处理器,这个配置将作用于当前整个配置文件种所有的Bean。
测试程序:
@Testpublic void testBeanLifecycle7(){ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring7.xml");SpringBean7 springBean7 = applicationContext.getBean("springBean7", SpringBean7.class);System.out.println("第六步:使用Bean:" + springBean7);//必须手动关闭Spring容器,注意Spring容器才会销毁Bean//applicationContext没有close这个方法,这个方法在ClassPathXmlApplicationContext里面,需要进行转型((ClassPathXmlApplicationContext) applicationContext).close();}
Bean生命周期之十大步:多了三个点位
这三个点位的特点:都是在检查你这个Bean是否实现了某些特定的接口,如果实现了这些接口,则Spring容器会调用这个接口中的方法。
点位1:在“Bean后处理器”before方法之前
检查Bean是否实现了Aware相关的接口,如果实现了接口则调用这些接口中的方法。
然后调用这些方法的目的是为了给你传递一些数据,让你更加方便使用。
Aware相关的接口包括:BeanNameAware、BeanClassLoaderAware、BeanFactoryAware
public interface BeanNameAware extends Aware {void setBeanName(String var1);
}
public interface BeanClassLoaderAware extends Aware {void setBeanClassLoader(ClassLoader var1);
}
public interface BeanFactoryAware extends Aware {void setBeanFactory(BeanFactory var1) throws BeansException;
}
点位2:在“Bean后处理器”before方法之后
检查Bean是否实现了InitializingBean接口,如果实现了,则调用接口中的方法。
public interface InitializingBean {void afterPropertiesSet() throws Exception;
}
点位3:使用Bean之后,或者说销毁Bean之前
检查Bean是否实现了DisposableBean接口,如果实现了,则调用接口中的方法。
public interface DisposableBean {void destroy() throws Exception;
}
定义一个Bean,分别实现这些接口:SpringBean10类
public class SpringBean10 implements BeanNameAware, BeanClassLoaderAware, BeanFactoryAware,InitializingBean,DisposableBean {private String name;public void setName(String name) {System.out.println("第二步:Bean属性赋值。");this.name = name;}public SpringBean10() {System.out.println("第一步:实例化Bean。");}public void initBean(){//这个方法需要自己写自己配,不一定是这个方法名,方法名随意。System.out.println("第四步:初始化Bean。");}public void destroyBean(){//这个方法也需要自己写自己配,不一定是这个方法名,方法名随意。System.out.println("第七步:销毁Bean。");}/*setBeanClassLoader对应:BeanClassLoaderAware接口setBeanFactory对应:BeanFactoryAwaresetBeanName对应:BeanNameAwareafterPropertiesSet对应:InitializingBean我们自己写的方法destroy与DisposableBean接口的方法重了,所以我们把我们销毁方法改为destroyBean*/@Overridepublic void setBeanClassLoader(ClassLoader classLoader) {System.out.println("点位1:Bean的类加载器是:" + classLoader);}@Overridepublic void setBeanFactory(BeanFactory beanFactory) throws BeansException {System.out.println("点位1:生产Bean的工厂对象是:" + beanFactory);}@Overridepublic void setBeanName(String s) {System.out.println("点位1:Bean的名字是:" + s);}@Overridepublic void afterPropertiesSet() throws Exception {System.out.println("点位2:InitializingBean接口的afterPropertiesSet方法执行");}@Overridepublic void destroy() throws Exception {System.out.println("点位3:DisposableBean接口的destroy方法执行");}
}
Bean后处理器还是使用之前的SpringBeanPostProcessor
创建spring10.xml配置:
测试程序:
@Testpublic void testBeanLifecycle10(){ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring10.xml");SpringBean10 springBean10 = applicationContext.getBean("springBean10", SpringBean10.class);System.out.println("第六步:使用Bean:" + springBean10);//必须手动关闭Spring容器,注意Spring容器才会销毁Bean//applicationContext没有close这个方法,这个方法在ClassPathXmlApplicationContext里面,需要进行转型((ClassPathXmlApplicationContext) applicationContext).close();}
Spring 根据Bean的作用域来选择管理方式。
演示程序使用之前的Bean生命周期10步,只需要把Bean改成多例的即可,spring10配置:
再次运行程序:
只管理到了使用Bean
有些时候可能会遇到这样的需求,某个java对象是我们自己new的,然后我们希望这个对象被Spring容器管理,怎么实现?
两个步骤:
创建一个Bean:Student类
public class Student {
}
测试程序:
@Testpublic void testRsgisterBean(){//自己new的对象Student student = new Student();System.out.println(student);//将自己new的对象纳入Spring容器进行管理//首先创建默认可列表BeanFactory对象DefaultListableBeanFactory factory = new DefaultListableBeanFactory();//然后调用相关方法把对象注册进去factory.registerSingleton("studentBean",student);//从Spring容器中获取Object studentBean = factory.getBean("studentBean");System.out.println(studentBean);}