最近打算写一个spring-mvc的插件,便于做接口测试,既然是插件,那就是零耦合。知道spring有几个接口,BeanPostProcessor, InitializingBean, DisposableBean, ApplicationContextAware, BeanFactoryPostProcessor,这几个接口也涉及到bean的生命周期。
贴代码:
调用类:
public interface Person {
void eatMeat();
}
public class Student implements Person {
private Fork fork;
public Student() {
System.out.println("Student.Constructor");
}
public Fork getFork() {
return fork;
}
public void setFork(Fork fork) {
this.fork = fork;
System.out.println("Student.setFork");
}
public void eatMeat() {
this.fork.forkMeat();
System.out.println("Student.eatMeat");
}
public void init() {
System.out.println("Student.init");
}
}
被依赖的类:
public interface Fork {
void forkMeat();
}
public class WoodFork implements Fork {
String name;
public String getName() {
return name;
}
public void setName(String name) {
System.out.println("WoodFork.setName");
this.name = name;
}
public WoodFork() {
System.out.println("WoodFork.Constructor");
}
@Override
public void forkMeat() {
System.out.println("WoodFork.forkMeat");
}
}
bean的几个接口实现类:
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class MyBeanPostProcessor implements BeanPostProcessor, InitializingBean, DisposableBean, ApplicationContextAware, BeanFactoryPostProcessor {
public String name;
public void setName(String name) {
this.name = name;
System.out.println("MyBeanPostProcessor.setName");
}
public MyBeanPostProcessor() {
System.out.println("MyBeanPostProcessor.Constructor");
}
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
System.out.println("MyBeanPostProcessor.setApplicationContext");
}
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("MyBeanPostProcessor.postProcessBeforeInitialization for " + bean.getClass().getSimpleName());
return bean;
}
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("MyBeanPostProcessor.postProcessAfterInitialization for " + bean.getClass().getSimpleName());
return bean;
}
public void destroy() throws Exception {
System.out.println("MyBeanPostProcessor.destory");
}
public void afterPropertiesSet() throws Exception {
System.out.println("MyBeanPostProcessor.afterPropertiesSet");
}
@Override
public void postProcessBeanFactory(
ConfigurableListableBeanFactory beanFactory) throws BeansException {
System.out.println("MyBeanPostProcessor.postProcessBeanFactory");
}
}
spring配置:
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd " >
<bean id="woodFork" class="com.kaige.spring.WoodFork" >
<property name="name" value="wood"></property>
</bean>
<bean id="student" class="com.kaige.spring.Student" init-method="init">
<property name="fork" ref="woodFork"></property>
</bean>
<bean class="com.kaige.spring.MyBeanPostProcessor">
<property name="name" value="processor"></property>
</bean>
</beans>
测试:
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
Student s = (Student)ctx.getBean("student");
s.eatMeat();
ctx.registerShutdownHook();
System.out.println("end");
}
}
结果:
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
