赞
踩
<?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
①祁连山
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。