当前位置: 首页 > news >正文

伤豆丁文库网站开发贵州网站备案局

伤豆丁文库网站开发,贵州网站备案局,建设一个网站需要哪方面的费用,如何创建一个属于自己的网站1、Spring事件监听器使用 Spring事件监听体系包括三个组件#xff1a;事件、事件监听器#xff0c;事件广播器 事件#xff1a;定义事件类型和事件源#xff0c;需要继承ApplicationEvent import org.springframework.context.ApplicationEvent; public class OrderEvent…1、Spring事件监听器使用 Spring事件监听体系包括三个组件事件、事件监听器事件广播器 事件定义事件类型和事件源需要继承ApplicationEvent import org.springframework.context.ApplicationEvent; public class OrderEvent extends ApplicationEvent {private String name;public OrderEvent(Object source,String name) {super(source);this.name name;}public String getName() {return name;}public void setName(String name) {this.name name;} }事件监听器用来监听某一类的事件并且执行具体业务逻辑需要实现ApplicationListener 接口或者需要用ListenerEvent(T)注解。下面分别有两种实现 import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component;Component public class OrderEventListener implements ApplicationListenerOrderEvent {Overridepublic void onApplicationEvent(OrderEvent event) {if(event.getName().equals(下订单)){System.out.println(下单已完成...);}} }import org.springframework.context.ApplicationListener; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Component; Component public class OrderEventListenerByAnnotation {EventListener(OrderEvent.class)public void onApplicationEvent(OrderEvent event) {if(event.getName().equals(下订单)){System.out.println(下单已完成...);}} }事件多播器负责广播通知所有监听器所有的事件监听器都注册在了事件多播器中。好比观察者模式中的被观察者。Spring容器默认生成的是同步事件多播器。可以自定义事件多播器定义为异步方式。即这个事件多播器可以不定义直接用默认的就行。也可以自定义执行相关需要的逻辑 import org.springframework.context.event.ApplicationEventMulticaster; import org.springframework.context.event.SimpleApplicationEventMulticaster; import org.springframework.core.task.SimpleAsyncTaskExecutor; import org.springframework.stereotype.Component; Configuration ComponentScan(value com.ybe) public class Config {Beanpublic ApplicationEventMulticaster applicationEventMulticaster(){SimpleApplicationEventMulticaster eventMulticaster new SimpleApplicationEventMulticaster();eventMulticaster.setTaskExecutor(new SimpleAsyncTaskExecutor());// todoreturn eventMulticaster;} }2、Spring事件源码分析 1.创建多播器 创建AnnotationConfigApplicationContext 的过程中会执行refresh()中的initApplicationEventMulticaster()方法。该方法先获取bean工厂然后判断工厂是否包含了beanName 为 applicationEventMulticaster的bean。如果包含了则获取该bean赋值给applicationEventMulticaster 属性。如果没有则创建一个 SimpleApplicationEventMulticaster 对象并且赋值给 applicationEventMulticaster 。实现了源码如下 /*** Initialize the ApplicationEventMulticaster.* Uses SimpleApplicationEventMulticaster if none defined in the context.* see org.springframework.context.event.SimpleApplicationEventMulticaster*/ protected void initApplicationEventMulticaster() {// 获取当前bean工厂,一般是DefaultListableBeanFactoryConfigurableListableBeanFactory beanFactory getBeanFactory();// 判断容器中是否存在bdName为applicationEventMulticaster的bd//也就是说自定义的事件监听多路广播器必须实现 ApplicationEventMulticaster接口if (beanFactory.containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) {// 如果有则从bean工厂得到这个bean对象this.applicationEventMulticaster beanFactory.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);if (logger.isTraceEnabled()) {logger.trace(Using ApplicationEventMulticaster [ this.applicationEventMulticaster ]);}}else {// 如果没有则默认采用SimpleApplicationEventMulticasterthis.applicationEventMulticaster new SimpleApplicationEventMulticaster(beanFactory);beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster);if (logger.isTraceEnabled()) {logger.trace(No APPLICATION_EVENT_MULTICASTER_BEAN_NAME bean, using [ this.applicationEventMulticaster.getClass().getSimpleName() ]);}}}注册监听器 监听器的注册有两种通过实现 ApplicationListener接口或者添加EventListener注解。 一.通过接口方式注册。实现接口 ApplicationListener。 注册的逻辑实现在refresh()中的registerListeners()方法里面。第一步先获取当前ApplicationContext中已经添加的 applicationListenersSpringMVCspringboot的run启动流程源码中有用到遍历添加到多播器中。第二步获取实现了ApplicationListener接口的listenerBeanNames集合添加至多播器中。第三步判断是否有早期事件如果有则发起广播。 protected void registerListeners() {// Register statically specified listeners first.// 遍历应用程序中存在的监听器集合并将对应的监听器添加到监听器的多路广播器中for (ApplicationListener? listener : getApplicationListeners()) {getApplicationEventMulticaster().addApplicationListener(listener);}// Do not initialize FactoryBeans here: We need to leave all regular beans// uninitialized to let post-processors apply to them!// 从容器中获取所有实现了ApplicationListener接口的bd的bdName// 放入ApplicationListenerBeans集合中String[] listenerBeanNames getBeanNamesForType(ApplicationListener.class, true, false);for (String listenerBeanName : listenerBeanNames) {getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);}// Publish early application events now that we finally have a multicaster...// 此处先发布早期的监听器集合SetApplicationEvent earlyEventsToProcess this.earlyApplicationEvents;this.earlyApplicationEvents null;if (!CollectionUtils.isEmpty(earlyEventsToProcess)) {for (ApplicationEvent earlyEvent : earlyEventsToProcess) {getApplicationEventMulticaster().multicastEvent(earlyEvent);}}}上面的代码中第二步为啥添加的是listenerBeanName 如果监听器是懒加载的话即有Lazy 注解。那么在这个时候创建监听器显然是不对的这个时候不能创建监听器。所以添加监听器到多播器的具体逻辑放在初始化具体的监听器之后。通过 BeanPostProcessor 的接口实现。具体的实现类是ApplicationListenerDetector 。这个类是在 refreah()中prepareBeanFactory()方法中添加的。代码如下 protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {// Tell the internal bean factory to use the contexts class loader etc.beanFactory.setBeanClassLoader(getClassLoader());if (!shouldIgnoreSpel) {beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));}beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));// Configure the bean factory with context callbacks.beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));beanFactory.ignoreDependencyInterface(EnvironmentAware.class);beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);beanFactory.ignoreDependencyInterface(MessageSourceAware.class);beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);beanFactory.ignoreDependencyInterface(ApplicationStartupAware.class);// BeanFactory interface not registered as resolvable type in a plain factory.// MessageSource registered (and found for autowiring) as a bean.beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);beanFactory.registerResolvableDependency(ResourceLoader.class, this);beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);beanFactory.registerResolvableDependency(ApplicationContext.class, this);// Register early post-processor for detecting inner beans as ApplicationListeners.// 添加 监听器后置处理器在初始化具体的实现了 ApplicationListener 接口的Bean之后进行调用。调用的是// postProcessAfterInitialization()方法。beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));// Detect a LoadTimeWeaver and prepare for weaving, if found.if (!NativeDetector.inNativeImage() beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));// Set a temporary ClassLoader for type matching.beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));}// Register default environment beans.if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) {beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment());}if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) {beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties());}if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) {beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment());}if (!beanFactory.containsLocalBean(APPLICATION_STARTUP_BEAN_NAME)) {beanFactory.registerSingleton(APPLICATION_STARTUP_BEAN_NAME, getApplicationStartup());}}二、通过注解的方式注册。EventListener(T)。 在创建AnnotationConfigApplicationContext 的构造方法中会执行org.springframework.context.annotation.AnnotationConfigUtils#registerAnnotationConfigProcessors(org.springframework.beans.factory.support.BeanDefinitionRegistry, java.lang.Object) 方法。这个方法中会添加两个 beanDefs, 代码如下 if (!registry.containsBeanDefinition(EVENT_LISTENER_PROCESSOR_BEAN_NAME)) {RootBeanDefinition def new RootBeanDefinition(EventListenerMethodProcessor.class);def.setSource(source);beanDefs.add(registerPostProcessor(registry, def, EVENT_LISTENER_PROCESSOR_BEAN_NAME)); }if (!registry.containsBeanDefinition(EVENT_LISTENER_FACTORY_BEAN_NAME)) {RootBeanDefinition def new RootBeanDefinition(DefaultEventListenerFactory.class);def.setSource(source);beanDefs.add(registerPostProcessor(registry, def, EVENT_LISTENER_FACTORY_BEAN_NAME)); }EventListenerMethodProcessor事件监听器的BeanFactory后置处理器在前期会创建 DefaultEventListenerFactory 后期在创建好Bean之后根据 EventListener 属性调用DefaultEventListenerFactory创建具体的 ApplicationListenerMethodAdapter 。DefaultEventListenerFactory监听器的创建工厂用来创建 ApplicationListenerMethodAdapter 。EventListenerMethodProcessor 的类继承图如下 在refreash的invokeBeanFactoryPostProcessors()中会调用 org.springframework.context.event.EventListenerMethodProcessor#postProcessBeanFactory方法获取EventListenerFactory 类型的 Bean。代码如下 Overridepublic void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {this.beanFactory beanFactory;// 获取或创建 EventListenerFactory 类型的 BeanMapString, EventListenerFactory beans beanFactory.getBeansOfType(EventListenerFactory.class, false, false);ListEventListenerFactory factories new ArrayList(beans.values());AnnotationAwareOrderComparator.sort(factories);this.eventListenerFactories factories;}在 org.springframework.beans.factory.support.DefaultListableBeanFactory#preInstantiateSingletons 方法中创建完所有的单例Bean 之后会遍历所有Bean是否实现了 SmartInitializingSingleton 接口。如果实现接口会执行该 Bean 的 afterSingletonsInstantiated() 方法。代码如下 public void preInstantiateSingletons() throws BeansException {if (logger.isTraceEnabled()) {logger.trace(Pre-instantiating singletons in this);}// Iterate over a copy to allow for init methods which in turn register new bean definitions.// While this may not be part of the regular factory bootstrap, it does otherwise work fine.// 将所有BeanDefinition的名字创建一个集合ListString beanNames new ArrayList(this.beanDefinitionNames);// Trigger initialization of all non-lazy singleton beans...// 触发所有非延迟加载单例bean的初始化遍历集合的对象for (String beanName : beanNames) {// 合并父类BeanDefinitionRootBeanDefinition bd getMergedLocalBeanDefinition(beanName);// 条件判断抽象单例非懒加载if (!bd.isAbstract() bd.isSingleton() !bd.isLazyInit()) {// 判断是否实现了FactoryBean接口if (isFactoryBean(beanName)) {// 根据beanName来获取具体的对象Object bean getBean(FACTORY_BEAN_PREFIX beanName);// 进行类型转换if (bean instanceof FactoryBean) {FactoryBean? factory (FactoryBean?) bean;// 判断这个FactoryBean是否希望立即初始化boolean isEagerInit;if (System.getSecurityManager() ! null factory instanceof SmartFactoryBean) {isEagerInit AccessController.doPrivileged((PrivilegedActionBoolean) ((SmartFactoryBean?) factory)::isEagerInit,getAccessControlContext());}else {isEagerInit (factory instanceof SmartFactoryBean ((SmartFactoryBean?) factory).isEagerInit());}// 如果希望急切的初始化则通过beanName获取bean实例if (isEagerInit) {getBean(beanName);}}}else {// 如果beanName对应的bean不是FactoryBean只是普通的bean通过beanName获取bean实例getBean(beanName);}}}// Trigger post-initialization callback for all applicable beans...// 遍历beanNames触发所有SmartInitializingSingleton的后初始化回调for (String beanName : beanNames) {// 获取beanName对应的bean实例Object singletonInstance getSingleton(beanName);// 判断singletonInstance是否实现了SmartInitializingSingleton接口if (singletonInstance instanceof SmartInitializingSingleton) {// 类型转换SmartInitializingSingleton smartSingleton (SmartInitializingSingleton) singletonInstance;// 触发SmartInitializingSingleton实现类的afterSingletonsInstantiated方法if (System.getSecurityManager() ! null) {AccessController.doPrivileged((PrivilegedActionObject) () - {smartSingleton.afterSingletonsInstantiated();return null;}, getAccessControlContext());}else {smartSingleton.afterSingletonsInstantiated();}}}}org.springframework.context.event.EventListenerMethodProcessor#afterSingletonsInstantiated 中会调用私有方法 processBean()进行 ApplicationEventAdatper 的创建。代码如下 /*** 该方法拿到某个bean的名称和它的目标类再这个范围上检测EventListener注解方法生成和注册 ApplicationListenerMethodAdapter 实例* param beanName* param targetType*/private void processBean(final String beanName, final Class? targetType) {if (!this.nonAnnotatedClasses.contains(targetType) AnnotationUtils.isCandidateClass(targetType, EventListener.class) !isSpringContainerClass(targetType)) {MapMethod, EventListener annotatedMethods null;try {// 检测当前类targetType上使用了注解EventListener的方法annotatedMethods MethodIntrospector.selectMethods(targetType,(MethodIntrospector.MetadataLookupEventListener) method -AnnotatedElementUtils.findMergedAnnotation(method, EventListener.class));}catch (Throwable ex) {// An unresolvable type in a method signature, probably from a lazy bean - lets ignore it.if (logger.isDebugEnabled()) {logger.debug(Could not resolve methods for bean with name beanName , ex);}}if (CollectionUtils.isEmpty(annotatedMethods)) {// 如果当前类targetType中没有任何使用了注解EventListener的方法则将该类保存到缓存nonAnnotatedClasses从而// 避免当前处理方法重入该类避免二次处理this.nonAnnotatedClasses.add(targetType);if (logger.isTraceEnabled()) {logger.trace(No EventListener annotations found on bean class: targetType.getName());}}else {// Non-empty set of methods// 如果当前类targetType中有些方法使用了注解EventListener那么根据方法上的信息对应的创建和注册ApplicationListener实例ConfigurableApplicationContext context this.applicationContext;Assert.state(context ! null, No ApplicationContext set);// 此处使用了this.eventListenerFactories,这些EventListenerFactory是在该类postProcessBeanFactory方法调用时被记录的ListEventListenerFactory factories this.eventListenerFactories;Assert.state(factories ! null, EventListenerFactory List not initialized);for (Method method : annotatedMethods.keySet()) {for (EventListenerFactory factory : factories) {if (factory.supportsMethod(method)) {Method methodToUse AopUtils.selectInvocableMethod(method, context.getType(beanName));// 如果当前EventListenerFactory支持处理该EventListener注解的方法则使用它创建 ApplicationListenerMethodAdapterApplicationListener? applicationListener factory.createApplicationListener(beanName, targetType, methodToUse);if (applicationListener instanceof ApplicationListenerMethodAdapter) {((ApplicationListenerMethodAdapter) applicationListener).init(context, this.evaluator);}// 将创建的ApplicationListener加入到容器中context.addApplicationListener(applicationListener);break;}}}if (logger.isDebugEnabled()) {logger.debug(annotatedMethods.size() EventListener methods processed on bean beanName : annotatedMethods);}}}}多播器广播事件 可以通过调用org.springframework.context.support.AbstractApplicationContext#publishEvent(java.lang.Object, org.springframework.core.ResolvableType) 方法进行事件的调用。代码如下 /*** 将给定事件发布到所有监听器*/protected void publishEvent(Object event, Nullable ResolvableType eventType) {// 如果event为null抛出异常Assert.notNull(event, Event must not be null);// Decorate event as an ApplicationEvent if necessary// 装饰事件作为一个应用事件如果有必要ApplicationEvent applicationEvent;// 如果event是ApplicationEvent的实例if (event instanceof ApplicationEvent) {// 将event强转为ApplicationEvent对象applicationEvent (ApplicationEvent) event;}else {// PayloadApplicationEvent携带任意有效负载的ApplicationEvent。// 创建一个新的PayloadApplicationEventapplicationEvent new PayloadApplicationEvent(this, event);// 如果eventType为 nullif (eventType null) {// 将applicationEvent转换为PayloadApplicationEvent 象引用其ResolvableType对象eventType ((PayloadApplicationEvent?) applicationEvent).getResolvableType();}}// Multicast right now if possible - or lazily once the multicaster is initialized// 如果可能的话现在就进行组播——或者在组播初始化后延迟// earlyApplicationEvents在多播程序设置之前发布的ApplicationEvent// 如果earlyApplicationEvents不为 null这种情况只在上下文的多播器还没有初始化的情况下才会成立会将applicationEvent// 添加到earlyApplicationEvents保存起来待多博器初始化后才继续进行多播到适当的监听器if (this.earlyApplicationEvents ! null) {//将applicationEvent添加到 earlyApplicationEventsthis.earlyApplicationEvents.add(applicationEvent);}else {// 多播applicationEvent到适当的监听器getApplicationEventMulticaster().multicastEvent(applicationEvent, eventType);}// Publish event via parent context as well...// 通过父上下文发布事件// 如果parent不为nullif (this.parent ! null) {// 如果parent是AbstractApplicationContext的实例if (this.parent instanceof AbstractApplicationContext) {// 将event多播到所有适合的监听器。如果event不是ApplicationEvent实例会将其封装成PayloadApplicationEvent对象再进行多播((AbstractApplicationContext) this.parent).publishEvent(event, eventType);}else {// 通知与event事件应用程序注册的所有匹配的监听器this.parent.publishEvent(event);}}}SimpleApplicationEventMulticaster 中的 multicasEventinvokeListenerdoInvokeListener 三个方法代码如下 /*** 将给定事件发布到所有监听器*/protected void publishEvent(Object event, Nullable ResolvableType eventType) {// 如果event为null抛出异常Assert.notNull(event, Event must not be null);// Decorate event as an ApplicationEvent if necessary// 装饰事件作为一个应用事件如果有必要ApplicationEvent applicationEvent;// 如果event是ApplicationEvent的实例if (event instanceof ApplicationEvent) {// 将event强转为ApplicationEvent对象applicationEvent (ApplicationEvent) event;}else {// PayloadApplicationEvent携带任意有效负载的ApplicationEvent。// 创建一个新的PayloadApplicationEventapplicationEvent new PayloadApplicationEvent(this, event);// 如果eventType为 nullif (eventType null) {// 将applicationEvent转换为PayloadApplicationEvent 象引用其ResolvableType对象eventType ((PayloadApplicationEvent?) applicationEvent).getResolvableType();}}// Multicast right now if possible - or lazily once the multicaster is initialized// 如果可能的话现在就进行组播——或者在组播初始化后延迟// earlyApplicationEvents在多播程序设置之前发布的ApplicationEvent// 如果earlyApplicationEvents不为 null这种情况只在上下文的多播器还没有初始化的情况下才会成立会将applicationEvent// 添加到earlyApplicationEvents保存起来待多博器初始化后才继续进行多播到适当的监听器if (this.earlyApplicationEvents ! null) {//将applicationEvent添加到 earlyApplicationEventsthis.earlyApplicationEvents.add(applicationEvent);}else {// 多播applicationEvent到适当的监听器getApplicationEventMulticaster().multicastEvent(applicationEvent, eventType);}// Publish event via parent context as well...// 通过父上下文发布事件// 如果parent不为nullif (this.parent ! null) {// 如果parent是AbstractApplicationContext的实例if (this.parent instanceof AbstractApplicationContext) {// 将event多播到所有适合的监听器。如果event不是ApplicationEvent实例会将其封装成PayloadApplicationEvent对象再进行多播((AbstractApplicationContext) this.parent).publishEvent(event, eventType);}else {// 通知与event事件应用程序注册的所有匹配的监听器this.parent.publishEvent(event);}}}protected void invokeListener(ApplicationListener? listener, ApplicationEvent event) {// 获取此多播器的当前错误处理程序ErrorHandler errorHandler getErrorHandler();// 如果errorHandler不为nullif (errorHandler ! null) {try {// 回调listener的onApplicationEvent方法传入eventdoInvokeListener(listener, event);}catch (Throwable err) {// 交给errorHandler接收处理errerrorHandler.handleError(err);}}else {// 回调listener的onApplicationEvent方法传入eventdoInvokeListener(listener, event);}}/*** 回调listener的onApplicationEvent方法传入 event* param listener* param event*/SuppressWarnings({rawtypes, unchecked})private void doInvokeListener(ApplicationListener listener, ApplicationEvent event) {try {//回调listener的onApplicationEvent方法传入 event:contextrefreshListener:onapplicaitonEvent:FrameworkServlet.this.onApplicationEvent()listener.onApplicationEvent(event);}catch (ClassCastException ex) {//获取异常信息String msg ex.getMessage();if (msg null || matchesClassCastMessage(msg, event.getClass())) {// Possibly a lambda-defined listener which we could not resolve the generic event type for// - lets suppress the exception and just log a debug message.Log logger LogFactory.getLog(getClass());if (logger.isTraceEnabled()) {logger.trace(Non-matching event type for listener: listener, ex);}}else {//抛出异常throw ex;}}}3、SpringMVC中事件使用 SpringMVC中就是通过Spring的事件机制进行九大组件的初始化。 ContextRefreshListener监听器的定义 监听器定义在FrameworkServlet类中作为内部类。代码如下 private class ContextRefreshListener implements ApplicationListenerContextRefreshedEvent {Overridepublic void onApplicationEvent(ContextRefreshedEvent event) {FrameworkServlet.this.onApplicationEvent(event);}}监听器的添加在 org.springframework.web.servlet.FrameworkServlet#configureAndRefreshWebApplicationContext 中进行。通过SourceFilteringListener进行包装。添加代码如下 // 添加监听器sourceFilteringListener到wac中,实际监听的是ContextRefreshListener所监听的事件监听ContextRefreshedEvent事件// 当接收到消息之后会调用onApplicationEvent方法调用onRefresh方法并将refreshEventReceived标志设置为true表示已经refresh过wac.addApplicationListener(new SourceFilteringListener(wac, new ContextRefreshListener()));多播器添加已经定义的ContextRefreshListener事件监听器 在refresh中的registerListeners方法进行添加代码如下 // Register statically specified listeners first.// 遍历应用程序中存在的监听器集合并将对应的监听器添加到监听器的多路广播器中for (ApplicationListener? listener : getApplicationListeners()) {getApplicationEventMulticaster().addApplicationListener(listener);}ContextRefreshListener事件监听器的触发 在refresh中的finishRefresh()方法中会调用publishEvnet(new ContextRefreshedEvent(this))发布事件。进行多播器广播代码如下 // 多播applicationEvent到适当的监听器 getApplicationEventMulticaster().multicastEvent(applicationEvent, eventType);最终会调到 FrameworkServlet.this.onApplicationEvent(event)。 public void onApplicationEvent(ContextRefreshedEvent event) {// 标记 refreshEventReceived 为truethis.refreshEventReceived true;synchronized (this.onRefreshMonitor) {// 处理事件中的 ApplicationContext 对象空实现子类DispatcherServlet会实现onRefresh(event.getApplicationContext());}}Overrideprotected void onRefresh(ApplicationContext context) {initStrategies(context);}protected void initStrategies(ApplicationContext context) {// 初始化 MultipartResolver:主要用来处理文件上传.如果定义过当前类型的bean对象那么直接获取如果没有的话可以为nullinitMultipartResolver(context);// 初始化 LocaleResolver:主要用来处理国际化配置,基于URL参数的配置(AcceptHeaderLocaleResolver)基于session的配置(SessionLocaleResolver)基于cookie的配置(CookieLocaleResolver)initLocaleResolver(context);// 初始化 ThemeResolver:主要用来设置主题ThemeinitThemeResolver(context);// 初始化 HandlerMapping:映射器用来将对应的request跟controller进行对应initHandlerMappings(context);// 初始化 HandlerAdapter:处理适配器主要包含Http请求处理器适配器简单控制器处理器适配器注解方法处理器适配器initHandlerAdapters(context);// 初始化 HandlerExceptionResolver:基于HandlerExceptionResolver接口的异常处理initHandlerExceptionResolvers(context);// 初始化 RequestToViewNameTranslator:当controller处理器方法没有返回一个View对象或逻辑视图名称并且在该方法中没有直接往response的输出流里面写数据的时候spring将会采用约定好的方式提供一个逻辑视图名称initRequestToViewNameTranslator(context);// 初始化 ViewResolver: 将ModelAndView选择合适的视图进行渲染的处理器initViewResolvers(context);// 初始化 FlashMapManager: 提供请求存储属性可供其他请求使用initFlashMapManager(context);}
http://www.zqtcl.cn/news/884273/

相关文章:

  • 做网站的注意什么北京建设协会网站首页
  • 石家庄网站开发设计网站建设重点步骤
  • 推广思路及执行方案昆明百度seo
  • 太原公司网站建立可视化小程序开发工具
  • 怎么做网站的搜索引擎云主机有什么用
  • 淘宝客新增网站南宁百度seo优化
  • 建设厅网站合同备案在哪里网站备案本人承诺
  • 做方案的网站住房城乡建设部官网
  • 怎样在门户网站做 推广天水市建设银行官方网站
  • 温州建网站哪家强网站建设谈客户说什么
  • 网站的子域名怎么设置整站seo排名外包
  • 免费网站在哪下载苏州建设银行网站
  • 邹平 建设项目 网站公示怎样做网站卖自己的产品教程
  • 手机免费网站建设哪家公司好免费动态域名申请
  • 提升网站排名怎么提交自己的网站
  • cms网站开发phpwordpress有什么功能
  • 专业网站制作解决方案自己在家搭建服务器
  • 中小企业网站提供了什么英文营销网站建设
  • 玉环市建设工程检测中心网站网站建设服务的具体条件
  • 主机网站wampserver搭建网站
  • 建设银行网站点不进去深圳龙华区招聘网最新招聘信息
  • 网站建设公司现在还挣钱吗wordpress棋牌
  • 网站建设有什么技术自媒体平台哪个好
  • 可以建网站的软件南昌seo代理商
  • 手机网站建设宽度中小型企业网站模板
  • 网站开发需要的所有技术中信建设有限责任公司历任董事长
  • 安徽省建设干部学校网站首页做软件是什么工作
  • 图书馆网站设计方案安徽质量工程建设网站
  • 电子商务网站建设效果那个网站可以做链接
  • 怎样做投资与理财网站网页设计优秀案例分析