以上配置,Spring会默认采用XmlWebApplicationContext,如果要采用注解,可以实用如下配置
<context-param> <param-name>contextClass</param-name> <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value> </context-param> <context-param> <param-name>contextConfigLocation</param-name> <param-value>org.acy.anno.Beans</param-value> </context-param> 4 Bean生命周期Bean的作用范围
实例化Bean时所经历的一系列阶段
关键点:
Bean自身的方法:如实例化,setter,或者init method和destroy method
Bean级别生命周期接口方法: BeanNameAware BeanFactoryAware InitializingBean和DisposableBean
重点 容器级生命周期接口方法 InstantiationAwareBeanPostProcessor和BeanPostProcessor,这些接口的实现类为单独的组件,实现后注册到容器上即可。[属于插件机制]
可以注册多个同种接口的实现类,但是必须实现Ordered接口排序。
新版本中还加入了InstantiationAwareBeanPostProcessorAdapter抽象类
例子
public class Cat implements BeanFactoryAware,BeanNameAware,InitializingBean,DisposableBean { private String brand; private String color; private int maxSpeed; public Cat() { } public Cat(String brand, String color, int maxSpeed) { this.brand = brand; this.color = color; this.maxSpeed = maxSpeed; } public void introduce() { System.out.println(this.toString()); } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public int getMaxSpeed() { return maxSpeed; } public void setMaxSpeed(int maxSpeed) { this.maxSpeed = maxSpeed; } @Override public String toString() { return "Car{" + "brand='" + brand + '\'' + ", color='" + color + '\'' + ", maxSpeed=" + maxSpeed + '}'; } private BeanFactory beanFactory; public void setBeanFactory(BeanFactory beanFactory) throws BeansException { System.out.println("setBeanFactory"); this.beanFactory = beanFactory; } private String beanName; public void setBeanName(String s) { System.out.println("setBeanName"); this.beanName = s; } public void destroy() throws Exception { System.out.println("destroy"); } public void afterPropertiesSet() throws Exception { System.out.println("afterPropertiesSet"); } public void myInit(){ System.out.println("myInit"); this.setMaxSpeed(240); } public void myDestroy(){ System.out.println("myDestroy"); } } public class MyBeanPostProcessor implements BeanPostProcessor { public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { if ("cat".equals(beanName)) { Cat cat = (Cat) bean; if (cat.getColor() == null) { System.out.println("color 为空 设为黑色"); cat.setColor("black"); } } return bean; } public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if ("cat".equals(beanName)) { Cat cat = (Cat) bean; if (cat.getMaxSpeed() > 200) { System.out.println("speed great then 200"); cat.setMaxSpeed(200); } } return bean; } } public class MyInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter { @Override public Object postProcessBeforeInstantiation(Class beanClass, String beanName) throws BeansException { if ("cat".equals(beanName)){ System.out.println("postProcessBeforeInstantiation"); } return null; } @Override public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException { if ("cat".equals(beanName)){ System.out.println("postProcessAfterInstantiation"); } return true; } @Override public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException { if ("cat".equals(beanName)){ System.out.println("postProcessPropertyValues"); } return pvs; } } public class BeanCycle { public static void main(String[] args) { Resource res = new ClassPathResource("applicationContext.xml"); XmlBeanFactory beanFactory = new XmlBeanFactory(res); ConfigurableBeanFactory configurableBeanFactory = beanFactory; configurableBeanFactory.addBeanPostProcessor(new MyBeanPostProcessor()); configurableBeanFactory.addBeanPostProcessor(new MyInstantiationAwareBeanPostProcessor()); Cat cat = beanFactory.getBean("cat", Cat.class); System.out.println(cat); cat.setColor("yellow"); Cat c2 = beanFactory.getBean("cat", Cat.class); System.out.println(cat == c2); beanFactory.destroySingletons(); } }