Spring中的一些常用接口
Posted 网络终结者
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring中的一些常用接口相关的知识,希望对你有一定的参考价值。
一、ApplicationContextAware接口
public interface ApplicationContextAware extends Aware { void setApplicationContext(ApplicationContext var1) throws BeansException; }
二、ServletContextAware 接口
public interface ServletContextAware extends Aware { void setServletContext(ServletContext var1); }
三、InitializingBean 接口
public interface InitializingBean { void afterPropertiesSet() throws Exception; }
四、ApplicationListener<ApplicationEvent> 接口
public interface ApplicationListener<E extends ApplicationEvent> extends EventListener { void onApplicationEvent(E var1); }
执行顺序
@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"); } } }
运行时,输出的顺序如下:
1 => StartupListener.setApplicationContext
2 => StartupListener.setServletContext
3 => StartupListener.afterPropertiesSet
4.1 => MyApplicationListener.onApplicationEvent
4.2 => MyApplicationListener.onApplicationEvent
4.1 => MyApplicationListener.onApplicationEvent
以上是关于Spring中的一些常用接口的主要内容,如果未能解决你的问题,请参考以下文章