最近打算写一个spring-mvc的插件,便于做接口测试,既然是插件,那就是零耦合。知道spring有几个接口,BeanPostProcessor, InitializingBean, DisposableBean, ApplicationContextAware, BeanFactoryPostProcessor,这几个接口也涉及到bean的生命周期。
贴代码:
调用类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
public interface Person {
|
被依赖的类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
public interface Fork {
|
bean的几个接口实现类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
import org.springframework.beans.BeansException;
|
spring配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<? xml version = "1.0" encoding = "utf-8" ?>
|
测试:
1 2 3 4 5 6 7 8 9 10 |
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
结果:
MyBeanPostProcessor.Constructor
MyBeanPostProcessor.setName
MyBeanPostProcessor.setApplicationContext
MyBeanPostProcessor.afterPropertiesSet
MyBeanPostProcessor.postProcessBeanFactory
WoodFork.Constructor
WoodFork.setName
MyBeanPostProcessor.postProcessBeforeInitialization for WoodFork
MyBeanPostProcessor.postProcessAfterInitialization for WoodFork
Student.Constructor
Student.setFork
MyBeanPostProcessor.postProcessBeforeInitialization for Student
Student.init
MyBeanPostProcessor.postProcessAfterInitialization for Student
WoodFork.forkMeat
Student.eatMeat
end
MyBeanPostProcessor.destory