标签:
方法 | 用途 |
Bean自身的方法 | 这个包括了Bean本身调用的方法和通过配置文件中<bean>的init-method和destroy-method指定的方法 |
Bean级生命周期接口方法 | 这个包括了BeanNameAware、BeanFactoryAware、InitializingBean和DiposableBean这些接口的方法 |
容器级生命周期接口方法 | 这个包括了InstantiationAwareBeanPostProcessor 和 BeanPostProcessor 这两个接口实现,一般称它们的实现类为“后处理器”。 |
工厂后处理器接口方法 | 这个包括了AspectJWeavingEnabler, ConfigurationClassPostProcessor, CustomAutowireConfigurer等等非常有用的工厂后处理器接口的方法。工厂后处理器也是容器级的。在应用上下文装配配置文件之后立即调用。 |
package com.sjf.bean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
* Student实体类
* @author sjf0115
*
*/
public class Student implements BeanNameAware,BeanFactoryAware,InitializingBean,ApplicationContextAware, DisposableBean{
private String name;
private int age;
private String school;
// 构造器
public Student(){
System.out.println("constructor Student...");
}
public void setName(String name) {
this.name = name;
System.out.println("setName...");
}
public void setAge(int age) {
this.age = age;
System.out.println("setAge...");
}
public void setSchool(String school) {
this.school = school;
System.out.println("setSchool...");
}
public void myInit(){
System.out.println("init-method...");
}
public void myDestroy(){
System.out.println("destroy-method...");
}
// 来自于BeanNameAware接口
public void setBeanName(String arg0) {
System.out.println("setBeanName...[" + arg0 + "]");
}
// 来自于BeanFactoryAware接口
public void setBeanFactory(BeanFactory arg0) throws BeansException {
System.out.println("setBeanFactory...");
}
// 来自于InitializingBean
public void afterPropertiesSet() throws Exception {
System.out.println("afterPropertiesSet...");
}
// 来自于DisposableBean
public void destroy() throws Exception {
System.out.println("destroy...");
}
// 来自于ApplicationContextAware接口
public void setApplicationContext(ApplicationContext arg0) throws BeansException {
System.out.println("setApplicationContext...");
}
@Override
public String toString() {
return "name:" + name + " age:" + age + " school:" + school;
}
}
package com.sjf.bean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
/**
* 自定义BeanPostProcessor
* @author sjf0115
*
*/
public class MyBeanPostProcessor implements BeanPostProcessor{
public MyBeanPostProcessor(){
System.out.println("constructor MyBeanPostProcessor...");
}
// 来自于BeanPostProcessor接口
public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException {
System.out.println("postProcessAfterInitialization...[" + arg1 + "]");
return arg0;
}
// 来自于BeanPostProcessor接口
public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException {
System.out.println("postProcessBeforeInitialization...[" + arg1 + "]");
return arg0;
}
}
<?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 = "yoona" class = "com.sjf.bean.Student" scope="singleton" init-method="myInit" destroy-method="myDestroy">
<property name="name" value="yoona"/>
<property name="age" value="24"/>
<property name="school" value="西电"/>
</bean>
<bean id = "myBeanPostProcessor" class="com.sjf.bean.MyBeanPostProcessor"/>
</beans>
package com.sjf.bean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 测试类
* @author sjf0115
*
*/
public class Test {
private static ApplicationContext context;
private static Student stu;
public static void main(String[] args) {
context = new ClassPathXmlApplicationContext("applicationContext.xml");
// 2. 从IOC容器中获取Bean实例
stu = (Student)context.getBean("yoona");
// 3.调用listAllBook方法
System.out.println(stu.toString());
// 4.关闭容器
((ClassPathXmlApplicationContext) context).registerShutdownHook();
}
}
constructor MyBeanPostProcessor...
constructor Student...
setName...
setAge...
setSchool...
setBeanName...[yoona]
setBeanFactory...
setApplicationContext...
postProcessBeforeInitialization...[yoona]
afterPropertiesSet...
init-method...
postProcessAfterInitialization...[yoona]
name:yoona age:24 school:西电
destroy...
destroy-method...
标签:
原文地址:http://blog.csdn.net/sunnyyoona/article/details/50636097