Spring中Bean的生命周期

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

#准备工作

最近刚开始看韩顺平老师讲的SPRing基础,感觉听起来还是感觉比较好,也就教程中的案例做了实践,接下来记录一下,Spring中Bean的生命周期。

Spring的下载地址(我使用的是3.2.4):http://repo.springsource.org/libs-release-local/org/springframework/spring/

①创建工程

首先,用IDE创建一个java工程,在工程目录下创建lib目录,复制需要引用的jar包并添加到ClassPath,因为我用的是Intellij,可能操作上与Eclipse有些不同。

②添加项目依赖Jar包

这个版本和教程中讲的2.5.5也有差异,主要是下载来的jar包中没有一个完整的包可以引用,只有一个个分开的jar包,这就使没有接触过Spring的我有些迷茫,

因为包很多,不知道哪些需要哪些不需要,查了一下资料,说是引用spring-core-3.2.4.RELEASE和spring-beans-3.2.4.RELEASE就够了,但是经过实践,还

需要引入spring-context-3.2.4.RELEASE、spring-expression-3.2.4.RELEASE和写日志的包commons-logging-1.1.1(需要单独下载)。

结论(需要引用的Jar包):

spring-core-3.2.4.RELEASE |spring-beans-3.2.4.RELEASE |spring-context-3.2.4.RELEASE |spring-expression-3.2.4.RELEASE |commons-logging-1.1.1

③创建applicationContext.xml文件

到这一步又开始迷茫了,搜索一下找到复制过来,Intellij检查到没有使用的引用都是灰色的,所以就删除了未使用的,结果为:

<?xml version="1.0" encoding="utf-8"?><beansxmlns="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"></beans>

④创建类文件

User.java

1packagecom.spring.pojo;23importorg.springframework.beans.BeansException;4importorg.springframework.beans.factory.*;5importorg.springframework.context.ApplicationContext;6importorg.springframework.context.ApplicationContextAware;78publicclassUserimplementsBeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean, DisposableBean {910privateString name;11//private String gender;12//private Work service;1314publicUser() {15System.out.println("①Constructor -> invoked");16}1718publicUser(String name) {//, String gender, Work service) {19this.name =name;20//this.gender = gender;21//this.service = service;22}2324publicString getName() {25returnname;26}2728publicvoidsetName(String name) {29System.out.println("①setName -> invoked");30this.name =name;31}3233/**34public String getGender() {35return gender;36}3738public void setGender(String gender) {39this.gender = gender;40}4142public Work getService() {43return service;44}4546public void setService(Work service) {47this.service = service;48}49*/5051/**52* init-method53*/54publicvoidinitBean() {55System.out.println("①initBean -> invoked");56}5758publicvoidshow() {59System.out.println("①" +this.name);//+ ":" + this.gender + "\r\n" + service.getStatus());60}6162@Override63publicvoidsetBeanName(String s) {64System.out.println("①" +s);65}6667@Override68publicvoidsetApplicationContext(ApplicationContext applicationContext)throwsBeansException {69System.out.println("①" +applicationContext);70}7172@Override73publicvoidsetBeanFactory(BeanFactory beanFactory)throwsBeansException {74System.out.println("①" +beanFactory);75Work workService = (Work) beanFactory.getBean("work");76String status =workService.getStatus();77System.out.println("②" +status);78}7980@Override81publicvoidafterPropertiesSet()throwsException {82System.out.println("①afterPropertiesSet -> invoked");83}8485@Override86publicvoiddestroy()throwsException {87System.out.println("①DisposableBean's destroy -> invoked");88}8990publicvoidbeforeDestroy() {91System.out.println("①beforeDestroy -> invoked");92}93}

Work.java

1packagecom.spring.pojo;23importorg.springframework.beans.BeansException;4importorg.springframework.beans.factory.*;5importorg.springframework.context.ApplicationContext;6importorg.springframework.context.ApplicationContextAware;78publicclassWorkimplementsBeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean, DisposableBean {910publicString name;11publicString age;1213publicWork() {14System.out.println("②Constructor -> invoked");15}1617publicWork(String name, String age) {18this.name =name;19this.age =age;20}2122publicString getName() {23returnname;24}2526publicvoidsetName(String name) {27System.out.println("②setName -> invoked");28this.name =name;29}3031publicString getAge() {32returnage;33}3435publicvoidsetAge(String age) {36this.age =age;37}3839publicString getStatus() {40returnthis.name + ":" +this.age;41}4243publicvoidinitMethod() {44System.out.println("②initMethod -> invoked");45}4647@Override48publicvoidsetApplicationContext(ApplicationContext applicationContext)throwsBeansException {49System.out.println("②" +applicationContext);50}5152@Override53publicvoidsetBeanFactory(BeanFactory beanFactory)throwsBeansException {54System.out.println("②" +beanFactory);55}5657@Override58publicvoidsetBeanName(String s) {59System.out.println("②" +s);60}6162@Override63publicvoiddestroy()throwsException {64System.out.println("②destroy -> invoked");65}6667@Override68publicvoidafterPropertiesSet()throwsException {69System.out.println("②afterPropertiesSet");70}7172publicvoiddestroyMethod() {73System.out.println("②destroyMethod -> invoked");74}75}

BeanProcessor.java

1packagecom.spring.pojo;23importorg.springframework.beans.BeansException;4importorg.springframework.beans.factory.config.BeanPostProcessor;56publicclassBeanProcessorimplementsBeanPostProcessor {78@Override9publicObject postProcessBeforeInitialization(Object o, String s)throwsBeansException {10System.out.println("postProcessBeforeInitialization -> invoked");11returno;12}1314@Override15publicObject postProcessAfterInitialization(Object o, String s)throwsBeansException {16System.out.println("postProcessAfterInitialization -> invoked");17returno;18}19}

UserTest.java

1packagecom.spring.pojo;23importcom.spring.pojo.User;4importorg.springframework.context.support.AbstractApplicationContext;5importorg.springframework.context.support.ClassPathXmlApplicationContext;67publicclassUserTest {89publicstaticvoidmain(String[] args) {10AbstractApplicationContext context =newClassPathXmlApplicationContext("applicationContext.xml");11User service = (User) context.getBean("user");12service.show();13}14}

applicationContext.xml

<?xml version="1.0" encoding="utf-8"?><beansxmlns="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"><beanid="user"class="com.spring.pojo.User"scope="prototype"init-method="initBean"destroy-method="beforeDestroy"><propertyname="name"value="祁连山"/><!--<property name="gender" value="男"/>

<property name="service" ref="workService"/>--></bean><beanid="work"class="com.spring.pojo.Work"scope="prototype"><propertyname="name"value="android开发"/><propertyname="age"value="三年"/></bean><beanid="beanProcessor"class="com.spring.pojo.BeanProcessor"/></beans>

⑤测试结果:

①Constructor -> invoked

①setName -> invoked

①user

①org.springframework.beans.factory.support.DefaultListableBeanFactory@3fcd000a: defining beans [user,work,beanProcessor]; root of factory hierarchy

②Constructor -> invoked

②setName -> invoked

②work

②org.springframework.beans.factory.support.DefaultListableBeanFactory@3fcd000a: defining beans [user,work,beanProcessor]; root of factory hierarchy

②org.springframework.context.support.ClassPathXmlApplicationContext@7c4633e7: startup date [Sun Nov 29 13:26:05 CST 2015]; root of context hierarchy

postProcessBeforeInitialization -> invoked

②afterPropertiesSet

postProcessAfterInitialization -> invoked

②android开发:三年

①org.springframework.context.support.ClassPathXmlApplicationContext@7c4633e7: startup date [Sun Nov 29 13:26:05 CST 2015]; root of context hierarchy

postProcessBeforeInitialization -> invoked

①afterPropertiesSet -> invoked

①initBean -> invoked

postProcessAfterInitialization -> invoked

①祁连山

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航