Jetty/Spring初始化: web
jetty启动时注册一个QueuedThreadPool和一些connector和monitor,并初始化一个WebAppContext/ServletContextHandler,同时注册一个LifeCycleListener LifeCycleListener监听jetty生命周期事件 ServletContextHandler初始化时,注册一个两个重要对象 springcontextlistener ContextLoaderListener springservlet DispatcherServlet ContextLoaderListener载入spring组件 DispatcherServlet用spring组件处理全部url请求
Jetty/Spring api:spring
private void startJetty(int port) throws Exception { Server server = new Server(port); WebApplicationContext springContext = getContext(); DispatcherServlet springServlet = new DispatcherServlet(springContext); ContextLoaderListener springListener = new ContextLoaderListener(springContext); ServletContextHandler context = getServletContextHandler(springServlet, springContextLoader); server.setHandler(context); server.start(); server.join(); } private static ServletContextHandler getServletContextHandler(DispatcherServlet springServlet, ContextLoaderListener springListener) throws IOException { ServletContextHandler contextHandler = new ServletContextHandler(); contextHandler.setErrorHandler(null); contextHandler.setContextPath(CONTEXT_PATH); contextHandler.addServlet(new ServletHolder(springServlet), MAPPING_URL); contextHandler.addEventListener(springListener); contextHandler.setResourceBase(new ClassPathResource("webapp").getURI().toString()); return contextHandler; } private static WebApplicationContext getContext() { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.setConfigLocation(CONFIG_LOCATION); context.getEnvironment().setDefaultProfiles(DEFAULT_PROFILE); return context; }
Bean生命周期之初始化:
1.容器寻找Bean的定义信息而且将其实例化。
2.容器对Bean进行依赖注入,Spring按照Bean定义信息配置Bean的全部属性。
3.若是Bean实现了BeanNameAware接口,工厂调用Bean的setBeanName()方法传递Bean的id。
4.若是Bean实现了BeanFactoryAware接口,工厂调用setBeanFactory()方法传入工厂自身。
5.若是BeanPostProcessor和Bean关联,那么BeanPostProcessor的postProcessBeforeInitialzation()方法将被调用。
6.若是Bean指定了init-method方法,它将被调用。
7.若是BeanPostProcessor和Bean关联,那么BeanPostProcessor的postProcessAfterInitialization()方法将被调用。api
Bean生命周期之运行: Bean被应用系统使用了,而且将被保留在Bean Factory中直到它再也不须要。app
Bean生命周期之销毁:
1.若是Bean实现了DisposableBean接口,destory()方法被调用。
2.若是指定了定制的销毁方法,调用这个方法。webapp
Bean生命周期之主要回调:
Bean初始化回调(bean注入后的行为)
注解@PostConstruct->接口InitializingBean.afterPropertiesSet()->XML方法init-method
Bean销毁回调(bean销毁以前的行为 )
注解@PreDestroy->接口DisposableBean.destroy()->XML方法destroy-methodide
BeanFactory生命周期之主要回调:
Bean初始化回调(bean注入后的行为)
接口BeanFactoryPostProcessor.postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)post
SpringContext生命周期容器之主要回调:
Bean初始化回调(bean注入后的行为)
接口BeanPostProcessor.postProcessBeforeInitialization(Object o, String s)
接口BeanPostProcessor.postProcessAfterInitialization(Object o, String s)url
PropertyPlaceholderConfigurerspa
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations" value="classpath:xxx/jdbc.properties"/> </bean>
PropertyOverrideConfigurer code
<context:property-override location="classpath:override.properties"/>
重要接口类
LifeCycleListener: jetty生命周期事件监听器 WebApplicationInitializer/SpringBootServletInitializer: 自定义servlet FilterRegistrationBean+Filter: 自定义filter ApplicationListener: spring生命周期事件监听器 CommandLineRunner: jetty彻底启动后(spring彻底启动前)执行 ApplicationRunner