# spring示例 **Repository Path**: allenpwd/spring ## Basic Information - **Project Name**: spring示例 - **Description**: spring示例 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2019-08-20 - **Last Updated**: 2026-06-05 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ### bean生命周期的几个回调方式的顺序(在所有必要依赖配置完成后) ##### 初始化 - 注解@PostConstruct标注的方法 - 接口InitializingBean的实现方法afterPropertiesSet(不推荐,因为代码与接口耦合) - init-method属性指定的方法 - xml配置方式: - 注解方式:@Bean(initMethod="") - 接口SmartInitializingSingleton的实现方法afterSingletonsInstantiated ##### 销毁 - 注解@PreDestroy标注的方法 - 接口DisposableBean的实现方法destroy(不推荐,因为代码与接口耦合) ### ### profile #### 配置要激活的profile - ctx.getEnvironment().setActiveProfiles() - spring.profiles.active属性,多个用逗号分隔 #### 设置默认的profile,即没有设置profile时的值 - ctx.getEnvironment().setDefaultProfiles() - spring.profiles.default属性,多个用逗号分隔 ### spring事件与监听 #### 原理 spring中是通过ApplicationListener及ApplicationEventMulticaster来进行事件驱动开发的,即实现观察者设计模式或发布-订阅模式。 - ApplicationListener监听容器中发布的事件,只要事件发生,就触发监听器的回调,来完成事件驱动开发。属于观察者设计模式中的Observer对象。 - ApplicationEventMulticaster用来通知所有的观察者对象,属于观察者设计模式中的Subject对象。 #### Spring提供的标准事件 - Contextrefreshedevent:当 Applicationcontext被初始化或刷新时,触发该事件。 - Context Closedevent:当 Application Context被关闭时,触发该事件。容器被关闭时,其管理的所有单例Bean都被销毁。 - Requesthandleevent:在Web应用中,当一个HTP请求结東时触发该事件。 - Contextstartedevent:当容器调用 start()方法时,触发该事件。 - Contextstopevent:当容器调用stop)方法时,触发该事件。 ### WebSocket 可以让Web应用中的客户端和服务端建立全双工通信 ### 问题 #### Controller加上@ResponseBody请求返回406状态码 原因: - 要支持json解析,需要引入jackson相关依赖(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) - 需开启springmvc对json的读写支持;xml配置时加上即可 #### 文件上传问题 ##### (1)no multi-part configuration has been provided 原因:需要配置multipartResolver的bean,如果是CommonsMultipartResolver则还要引入commons-upload.jar依赖 #### jetty的maven插件运行项目时log4j报数组越界 处理方法:版本调低,tomcat启动不会有这个问题,贼鸡儿操蛋 #### websocket走nginx反向代理 ##### 报400 nginx反向代理加上配置 ```shell script proxy_http_version 1.1; #少了这个服务端报错Not a WebSocket handshake proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; ``` #### post方式,参数通过body传,但request.getInputStream()方式获取的数据为空 根据Servlet规范,如果同时满足下列条件,则请求体(Entity)中的表单数据,\ 将被填充到request的parameter集合中(request.getParameter系列方法可以读取相关数据) - 这是一个HTTP/HTTPS请求 - 请求方法是POST(querystring无论是否POST都将被设置到parameter中) - 请求的类型(Content-Type头)是application/x-www-form-urlencoded - Servlet调用了getParameter系列方法 这种情况下的表单数据已经被填充到parameterMap中,不能再通过getInputStream获取。