package com.provider.bean;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
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.support.ClassPathXmlApplicationContext;
public class Hello implements InitializingBean,DisposableBean,BeanNameAware,BeanFactoryAware{
private String name;
public void setName(String name) {
System.out.println("执行Hello: setName");
this.name = name;
}
public Hello(){
System.out.println("执行Hello: 构造方法");
}
@PostConstruct
public void postConstruct() {
System.out.println("执行Hello: postConstruct");
}
public void initMethod() {
System.out.println("执行Hello: init-method");
}
@PreDestroy
public void preDestroy() {
System.out.println("执行Hello: preDestroy");
}
public void destroy() throws Exception {
System.out.println("执行Hello: destroy");
}
public void destroyMethod() {
System.out.println("执行Hello: destroy-method");
}
public void afterPropertiesSet() throws Exception {
System.out.println("执行Hello: afterPropertiesSet");
}
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("执行Hello: setBeanFactory");
}
public void setBeanName(String name) {
System.out.println("执行Hello: setBeanName");
}
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-core.xml");
System.out.println(applicationContext.containsBean("hello"));
applicationContext.close();
}
}
<bean id="hello" class="com.provider.bean.Hello" init-method="initMethod" destroy-method="destroyMethod">
<property name="name" value="ppp"></property>
</bean>
执行Hello: 构造方法
执行Hello: setName
执行Hello: setBeanName
执行Hello: setBeanFactory
执行Hello: afterPropertiesSet
执行Hello: init-method
true
执行Hello: destroy
执行Hello: destroy-method
过程:
默认构造方法
set方法注入属性
BeanNameAware setBeanName
BeanFactoryAware setBeanFactory
BeanPostProcessor postProcessBeforeInitialization
InitializingBean afterPropertiesSet
init-method
BeanPostProcessor postProcessAfterInitialization
DisposableBean destroy
destroy-method