最近刚开始看韩顺平老师讲的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"?> <beans xmlns="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
1 package com.spring.pojo; 2 3 import org.springframework.beans.BeansException; 4 import org.springframework.beans.factory.*; 5 import org.springframework.context.ApplicationContext; 6 import org.springframework.context.ApplicationContextAware; 7 8 public class User implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean, DisposableBean { 9 10 private String name; 11 // private String gender; 12 // private Work service; 13 14 public User() { 15 System.out.println("①Constructor -> invoked"); 16 } 17 18 public User(String name) {// , String gender, Work service) { 19 this.name = name; 20 // this.gender = gender; 21 // this.service = service; 22 } 23 24 public String getName() { 25 return name; 26 } 27 28 public void setName(String name) { 29 System.out.println("①setName -> invoked"); 30 this.name = name; 31 } 32 33 /** 34 public String getGender() { 35 return gender; 36 } 37 38 public void setGender(String gender) { 39 this.gender = gender; 40 } 41 42 public Work getService() { 43 return service; 44 } 45 46 public void setService(Work service) { 47 this.service = service; 48 } 49 */ 50 51 /** 52 * init-method 53 */ 54 public void initBean() { 55 System.out.println("①initBean -> invoked"); 56 } 57 58 public void show() { 59 System.out.println("①" + this.name); // + ":" + this.gender + "\r\n" + service.getStatus()); 60 } 61 62 @Override 63 public void setBeanName(String s) { 64 System.out.println("①" + s); 65 } 66 67 @Override 68 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { 69 System.out.println("①" + applicationContext); 70 } 71 72 @Override 73 public void setBeanFactory(BeanFactory beanFactory) throws BeansException { 74 System.out.println("①" + beanFactory); 75 Work workService = (Work) beanFactory.getBean("work"); 76 String status = workService.getStatus(); 77 System.out.println("②" + status); 78 } 79 80 @Override 81 public void afterPropertiesSet() throws Exception { 82 System.out.println("①afterPropertiesSet -> invoked"); 83 } 84 85 @Override 86 public void destroy() throws Exception { 87 System.out.println("①DisposableBean's destroy -> invoked"); 88 } 89 90 public void beforeDestroy() { 91 System.out.println("①beforeDestroy -> invoked"); 92 } 93 }
Work.java
1 package com.spring.pojo; 2 3 import org.springframework.beans.BeansException; 4 import org.springframework.beans.factory.*; 5 import org.springframework.context.ApplicationContext; 6 import org.springframework.context.ApplicationContextAware; 7 8 public class Work implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean, DisposableBean { 9 10 public String name; 11 public String age; 12 13 public Work() { 14 System.out.println("②Constructor -> invoked"); 15 } 16 17 public Work(String name, String age) { 18 this.name = name; 19 this.age = age; 20 } 21 22 public String getName() { 23 return name; 24 } 25 26 public void setName(String name) { 27 System.out.println("②setName -> invoked"); 28 this.name = name; 29 } 30 31 public String getAge() { 32 return age; 33 } 34 35 public void setAge(String age) { 36 this.age = age; 37 } 38 39 public String getStatus() { 40 return this.name + ":" + this.age; 41 } 42 43 public void initMethod() { 44 System.out.println("②initMethod -> invoked"); 45 } 46 47 @Override 48 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { 49 System.out.println("②" + applicationContext); 50 } 51 52 @Override 53 public void setBeanFactory(BeanFactory beanFactory) throws BeansException { 54 System.out.println("②" + beanFactory); 55 } 56 57 @Override 58 public void setBeanName(String s) { 59 System.out.println("②" + s); 60 } 61 62 @Override 63 public void destroy() throws Exception { 64 System.out.println("②destroy -> invoked"); 65 } 66 67 @Override 68 public void afterPropertiesSet() throws Exception { 69 System.out.println("②afterPropertiesSet"); 70 } 71 72 public void destroyMethod() { 73 System.out.println("②destroyMethod -> invoked"); 74 } 75 }
BeanProcessor.java
1 package com.spring.pojo; 2 3 import org.springframework.beans.BeansException; 4 import org.springframework.beans.factory.config.BeanPostProcessor; 5 6 public class BeanProcessor implements BeanPostProcessor { 7 8 @Override 9 public Object postProcessBeforeInitialization(Object o, String s) throws BeansException { 10 System.out.println("postProcessBeforeInitialization -> invoked"); 11 return o; 12 } 13 14 @Override 15 public Object postProcessAfterInitialization(Object o, String s) throws BeansException { 16 System.out.println("postProcessAfterInitialization -> invoked"); 17 return o; 18 } 19 }
UserTest.java
1 package com.spring.pojo; 2 3 import com.spring.pojo.User; 4 import org.springframework.context.support.AbstractApplicationContext; 5 import org.springframework.context.support.ClassPathXmlApplicationContext; 6 7 public class UserTest { 8 9 public static void main(String[] args) { 10 AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); 11 User service = (User) context.getBean("user"); 12 service.show(); 13 } 14 }
applicationContext.xml
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="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"> <bean id="user" class="com.spring.pojo.User" scope="prototype" init-method="initBean" destroy-method="beforeDestroy"> <property name="name" value="祁连山"/> <!-- <property name="gender" value="男"/> <property name="service" ref="workService"/> --> </bean> <bean id="work" class="com.spring.pojo.Work" scope="prototype"> <property name="name" value="android开发"/> <property name="age" value="三年"/> </bean> <bean id="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
①祁连山