码迷,mamicode.com
首页 > 编程语言 > 详细

Spring中的一些常用接口

时间:2017-10-18 11:13:06      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:onclick   get   logger   override   throw   strong   nbsp   splay   start   

一、ApplicationContextAware接口

技术分享
public interface ApplicationContextAware extends Aware {
    void setApplicationContext(ApplicationContext var1) throws BeansException;
}
View Code

二、ServletContextAware 接口

技术分享
public interface ServletContextAware extends Aware {
    void setServletContext(ServletContext var1);
}
View Code

三、InitializingBean 接口

技术分享
public interface InitializingBean {
    void afterPropertiesSet() throws Exception;
}
View Code

四、ApplicationListener<ApplicationEvent> 接口

技术分享
public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
    void onApplicationEvent(E var1);
}
View Code

执行顺序

技术分享
@Component
public class StartupListener implements ApplicationContextAware, ServletContextAware,
        InitializingBean, ApplicationListener<ContextRefreshedEvent> {
 
    protected Logger logger = LogManager.getLogger();
 
    @Override
    public void setApplicationContext(ApplicationContext ctx) throws BeansException {
        logger.info("1 => StartupListener.setApplicationContext");
    }
 
    @Override
    public void setServletContext(ServletContext context) {
        logger.info("2 => StartupListener.setServletContext");
    }
 
    @Override
    public void afterPropertiesSet() throws Exception {
        logger.info("3 => StartupListener.afterPropertiesSet");
    }
 
    @Override
    public void onApplicationEvent(ContextRefreshedEvent evt) {
        logger.info("4.1 => MyApplicationListener.onApplicationEvent");
        if (evt.getApplicationContext().getParent() == null) {
            logger.info("4.2 => MyApplicationListener.onApplicationEvent");
        }
    }
 
}
View Code

运行时,输出的顺序如下:

1 => StartupListener.setApplicationContext
2 => StartupListener.setServletContext
3 => StartupListener.afterPropertiesSet
4.1 => MyApplicationListener.onApplicationEvent
4.2 => MyApplicationListener.onApplicationEvent
4.1 => MyApplicationListener.onApplicationEvent

 

Spring中的一些常用接口

标签:onclick   get   logger   override   throw   strong   nbsp   splay   start   

原文地址:http://www.cnblogs.com/feiyun126/p/7685490.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!