Spring-Callback

一. 初始化和销毁的Callback

执行顺序:注解->接口->xml

  • 方式1(继承接口):
    InitializaingBean
    DisposableBean
public class UserDao implements InitializingBean, DisposableBean {
    @Override
    public void destroy() throws Exception {
        
    }

    @Override
    public void afterPropertiesSet() throws Exception {

    }
}
  • 方式2(xml):
    bean.xml中使用init/destroy-method配置项
<bean id="speaker" class="ioc.Speaker" init-method="Speak" destroy-method="destroy" />
  • 方式3(注解):
public class UserDao {
    @PostConstruct
    public void init() throws Exception {

    }
    
    @PreDestroy
    public void destroy() throws Exception {
        
    }
}

二. Bean的Callback

  • ApplicationContextAware
    Bean继承接口后,实例化后调用setApplicationContext,把上下文传入,框架级别可能用到
    方法:void setApplicationContext(ApplicationContext)

  • BeanNameAware
    运行时候Bean的名字
    BeanNameAware
    方法:void setBeanName(String)

posted @ 2016-08-09 13:14  zhangshihai1232  阅读(170)  评论(0)    收藏  举报