spring框架学习笔记(八)
生活随笔
收集整理的這篇文章主要介紹了
spring框架学习笔记(八)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
bean的生命周期
在配置bean的時(shí)候指定 bean的初始化方法和析構(gòu)函數(shù)。
下面的例子展示了從Ioc容器創(chuàng)建到創(chuàng)建bean實(shí)例到Ioc容器銷毀的過程。
配置文件如下:
?
<bean id="flowerBean2" class="com.pfSoft.spel.Flower" init-method="testInitFun" destroy-method="testDestory"><property name="flowerName" value="rose"></property><property name="color" value="red"></property><property name="price" value="20"></property></bean>?將原實(shí)體類改寫,在構(gòu)造函數(shù)、屬性賦值、中增加out輸出,方便查看先后順序,并新增init和destory方法:
public class Flower {private String flowerName;private Double price;/*** * @return the flowerName*/public String getFlowerName() {return flowerName;}/*** @param flowerName the flowerName to set*/public void setFlowerName(String flowerName) {System.out.println("這里執(zhí)行屬性的set方法,setFlowName");this.flowerName = flowerName;}private String color;/*** * @return the color*/public String getColor() {return color;}/*** @param color the color to set*/public void setColor(String color) {System.out.println("這里執(zhí)行屬性的set方法,setcolor");this.color = color;}/*** * @return the price*/public Double getPrice() {return price;}/*** @param price the price to set*/public void setPrice(Double price) {this.price = price;}/* (non-Javadoc)* @see java.lang.Object#toString()*/@Overridepublic String toString() {return "Flower [flowerName=" + flowerName + ", price=" + price+ ", color=" + color + "]";}private Flower(){System.out.println("這里執(zhí)行構(gòu)造函數(shù)");}private void testInitFun() {System.out.println("這里執(zhí)行初始化方法");}private void testDestory() {System.out.println("這里執(zhí)行destory方法");}}?測試代碼如下:
ApplicationContext applicationContext;@Beforepublic void init() {applicationContext = new ClassPathXmlApplicationContext("spring-SpEL.xml");}@Testpublic void testFlower() {Flower flower = (Flower) applicationContext.getBean("flowerBean2");System.out.println(flower);}@Afterpublic void after() {ClassPathXmlApplicationContext context = (ClassPathXmlApplicationContext) applicationContext;context.close();}?輸出結(jié)果為:
這里執(zhí)行構(gòu)造函數(shù) 這里執(zhí)行屬性的set方法,setFlowName 這里執(zhí)行屬性的set方法,setcolor 這里執(zhí)行初始化方法 Flower [flowerName=rose, price=20.0, color=red] 四月 26, 2016 10:19:33 下午 org.springframework.context.support.ClassPathXmlApplicationContext doClose 信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@1637f22: startup date [Tue Apr 26 22:19:33 CST 2016]; root of context hierarchy 這里執(zhí)行destory方法由此可見執(zhí)行先后順序?yàn)?? bean的構(gòu)造函數(shù)——》set方法——》配置bean時(shí)指定的init-method方法——》銷毀時(shí),bean中配置指定的destroy-method方法
?
創(chuàng)建bean后置處理器
需要實(shí)現(xiàn)BeanPostProcessor接口,并且這個(gè)處理器是針對所有bean的。
?配置文件如下:
<bean id="flowerBean" class="com.pfSoft.spel.Flower" init-method="testInitFun" destroy-method="testDestory"><property name="flowerName" value="rose"></property><property name="color" value="red"></property><property name="price" value="20"></property></bean><bean class="com.pfSoft.spel.MyBeanProcess"></bean>Processor代碼如下,在這里可以對實(shí)例化bean之前進(jìn)行修改,返回的bean將是這里修改后的:
public Object postProcessBeforeInitialization(Object bean, String beanName)throws BeansException {return bean;}public Object postProcessAfterInitialization(Object bean, String beanName)throws BeansException {System.out.println("執(zhí)行BeforeInitialization");System.out.println(MessageFormat.format("修改前的bean為:{0}", bean));if("flowerBean".equals(beanName)){System.out.println("開始修改值");Flower flower= (Flower) bean;flower.setColor("白色");}System.out.println(MessageFormat.format("修改后的bean為:{0}", bean));return bean;}測試代碼如下:
@Beforepublic void init() {applicationContext = new ClassPathXmlApplicationContext("spring-SpEL.xml");}@Testpublic void testFlower() {com.pfSoft.spel.Flower flower = (Flower) applicationContext.getBean("flowerBean");System.out.println(MessageFormat.format("測試輸出flower:{0}", flower) );}@Afterpublic void after() {ClassPathXmlApplicationContext context = (ClassPathXmlApplicationContext) applicationContext;context.close();}?測試輸出如下:
這里執(zhí)行構(gòu)造函數(shù) 這里執(zhí)行屬性的set方法,setFlowName 這里執(zhí)行屬性的set方法,setcolor 這里執(zhí)行初始化方法 執(zhí)行BeforeInitialization 修改前的bean為:Flower [flowerName=rose, price=20.0, color=red] 開始修改值 這里執(zhí)行屬性的set方法,setcolor 修改后的bean為:Flower [flowerName=rose, price=20.0, color=白色] 測試輸出flower:Flower [flowerName=rose, price=20.0, color=白色] 四月 28, 2016 4:35:35 下午 org.springframework.context.support.ClassPathXmlApplicationContext doClose 信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@1637f22: startup date [Thu Apr 28 16:35:34 CST 2016]; root of context hierarchy 這里執(zhí)行destory方法?由上可以看出bean的生命周期,執(zhí)行過程。
?
轉(zhuǎn)載于:https://www.cnblogs.com/falcon-fei/p/5437170.html
總結(jié)
以上是生活随笔為你收集整理的spring框架学习笔记(八)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 移动app测试流程与测试点
- 下一篇: Mybatis增删改