#準備工作
最近剛開始看韓順平老師講的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-method53 */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 @Override63 public void setBeanName(String s) {64 System.out.println("①" + s);65 }66 67 @Override68 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {69 System.out.println("①" + applicationContext);70 }71 72 @Override73 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 @Override81 public void afterPropertiesSet() throws Exception {82 System.out.println("①afterPropertiesSet -> invoked");83 }84 85 @Override86 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 @Override48 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {49 System.out.println("②" + applicationContext);50 }51 52 @Override53 public void setBeanFactory(BeanFactory beanFactory) throws BeansException {54 System.out.println("②" + beanFactory);55 }56 57 @Override58 public void setBeanName(String s) {59 System.out.println("②" + s);60 }61 62 @Override63 public void destroy() throws Exception {64 System.out.println("②destroy -> invoked");65 }66 67 @Override68 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 @Override15 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
①祁連山
新聞熱點
疑難解答