一、生命周期
Spring容器中的生命周期方法分为4类:
- Bean自身方法
- Bean级生命周期接口方法
- 容器级生命周期接口方法
- 工厂级生命周期接口方法
1.1 Bean自身生命周期方法
Bean自身的方法:
- 构造函数实例化bean
- initMethod指定方法(指定某个方法在bean实例化完成,依赖关系结束后执行)
- destroyMethod指定方法(指定某个方法在bean销毁之前执行)
定义一个类:
@Component
public class XiaoChao {
@Value("小超")
private String name;
@Value("20")
private int age;
public XiaoChao() {
System.out.println("实例化bean===>构造器");
}
public void xiaochaoInit(){
System.out.println("bean自身方法===>初始化方法");
}
public void xiaochaoDestroy() {
System.out.println("bean自身方法====>销毁方法");
}
public void hello(){
System.out.println("bean自身方法===>helloWorld");
}
public void setName(String name) {
this.name = name;
System.out.println("bean的属性注入===>"+name);
}
public void setAge(int age) {
this.age = age;
System.out.println("bean的属性注入===>"+age);
}
}
配置bean
@Configuration
public class MyConfiguration {
//initMethod:指定初始化方法
//destroyMethod:指定销毁方法
@Bean(initMethod = "xiaochaoInit",destroyMethod = "xiaochaoDestroy")
public XiaoChao xiaoChao(){
return new XiaoChao();
}
}
1.2 Bean级生命周期接口方法
Bean级生命周期方法有:
- InitializingBean接口(bean实例化完成,属性值注入后调用,initMethod之前调用)
public interface InitializingBean {
/**
* Invoked by the containing {@code BeanFactory} after it has set all bean properties
* and satisfied {@link BeanFactoryAware}, {@code ApplicationContextAware} etc.
* <p>This method allows the bean instance to perform validation of its overall
* configuration and final initialization when all bean properties have been set.
* @throws Exception in the event of misconfiguration (such as failure to set an
* essential property) or if initialization fails for any other reason
*/
void afterPropertiesSet() throws Exception;
}
- DisposableBean接口(bean销毁前调用,destroyMethod之前调用)
public interface DisposableBean {
/**
* Invoked by the containing {@code BeanFactory} on destruction of a bean.
* @throws Exception in case of shutdown errors. Exceptions will get logged
* but not rethrown to allow other beans to release their resources as well.
*/
void destroy() throws Exception;
}
- BeanNameAware接口(bean实例化时给bean注入beanName)
public interface BeanNameAware extends Aware {
/**
* Set the name of the bean in the bean factory that created this bean.
* <p>Invoked after population of normal bean properties but before an
* init callback such as {@link InitializingBean#afterPropertiesSet()}
* or a custom init-method.
* @param name the name of the bean in the factory.
* Note that this name is the actual bean name used in the factory, which may
* differ from the originally specified name: in particular for inner bean
* names, the actual bean name might have been made unique through appending
* "#..." suffixes. Use the {@link BeanFactoryUtils#originalBeanName(String)}
* method to extract the original bean name (without suffix), if desired.
*/
void setBeanName(String name);
}
- ApplicationContextAware接口(bean实例化时给bean注入ApplicationContext)
public interface ApplicationContextAware extends Aware {
/**
* Set the ApplicationContext that this object runs in.
* Normally this call will be used to initialize the object.
* <p>Invoked after population of normal bean properties but before an init callback such
* as {@link org.springframework.beans.factory.InitializingBean#afterPropertiesSet()}
* or a custom init-method. Invoked after {@link ResourceLoaderAware#setResourceLoader},
* {@link ApplicationEventPublisherAware#setApplicationEventPublisher} and
* {@link MessageSourceAware}, if applicable.
* &