ITeye博客

spring

共 124 篇文章

spring session序列化问题排查

严重: Servlet.service() for servlet [spring] in context with path [/] threw exception org.springframework.data.redis.serializer.SerializationException: Cannot serialize; nested exception is org.springframework.core.serializer.support.SerializationFailedException: Failed to serialize object using Defa…

2017-12-01 19:07 · java · spring

spring mvc统一异常处理(@ControllerAdvice + @ExceptionHandler)

spring 封装了非常强大的异常处理机制。本文选取@ControllerAdvice + @ExceptionHandler 这种零配置(全注解),作为异常处理解决方案! @ControllerAdvice,是spring3.2提供的新注解,从名字上可以看出大体意思是控制器增强。让我们先看看@ControllerAdvice的实现: 1 2 3 4 5 @Target(value=TYPE) @Retention(value=RUNTIME) @Documented @Component public @interface ControllerAdvice (spring 官方解释) 即把…

2017-12-01 17:09 · spring

springboot注解

@RestController和@RequestMapping注解 我们的Example类上使用的第一个注解是 @RestController 。这被称为一个构造型(stereotype)注解。它为阅读代码的人们提供建议。对于Spring,该类扮演了一个特殊角色。在本示例中,我们的类是一个web @Controller ,所以当处理进来的web请求时,Spring会询问它。@RequestMapping 注解提供路由信息。它告诉Spring任何来自"/"路径的HTTP请求都应该被映射到 home 方法。 @RestController 注解告诉Spring以字符串的形式渲染结果,并直接返回给…

2017-12-01 09:44 · spring

Spring 4 xml 注解配置谅解

《Spring in Action》4th Edition 学习笔记 Spring 注入 bean 可以有如下三种方式: 自动注入 显式注入(java 配置) 显式注入(xml 配置) 自动注入 自动注入主要使用如下的技术: Component scanning:Spring 自动发现需要在应用上下文中生成的 bean Autowiring:自动注入 bean 依赖 配置可被发现的 beans 使用 @Component 注解配置可被发现的 Bean,默认名称为类名(首字母小写) package soundsystem; import org.springframework.stereoty…

2017-12-01 09:39 · spring

利用junit对springMVC的Controller进行测试

平时对junit测试service/DAO层已经很熟悉不过了,但是要测试controller层,不能总重启服务器吧,那么我们就用junit4模拟请求,测试controller层的方法。 代码示例: import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springf…

2017-11-30 16:26 · java · spring

spring cloud config实现datasource的热部署

关于spring cloud config的基本使用,前面的博客中已经说过了,如果不了解的话,请先看以前的博客 spring cloud config整合gitlab搭建分布式的配置中心 spring cloud config分布式配置中心的高可用 今天,我们的重点是如何实现数据源的热部署。 1、在客户端配置数据源 [java] view plain copy @RefreshScope @Configuration// 配置数据源 public class DataSourceConfigure { @Bean @RefreshScope// 刷新配置文件 @ConfigurationPr…

2017-11-21 16:44 · spring

Spring+MyBatis实现数据库读写分离方案

百度关键词:spring mybatis 多数据源 读写分离 推荐第四种 方案1 通过MyBatis配置文件创建读写分离两个DataSource,每个SqlSessionFactoryBean对象的mapperLocations属性制定两个读写数据源的配置文件。将所有读的操作配置在读文件中,所有写的操作配置在写文件中。 优点:实现简单 缺点:维护麻烦,需要对原有的xml文件进行重新修改,不支持多读,不易扩展 实现方式 <bean id="abstractDataSource" abstract="true" class="com.alibaba.druid.pool.DruidDataSou…

2017-11-20 17:15 · java · spring

Spring MVC-ContextLoaderListener和DispatcherServlet

Tomcat或Jetty作为Servlet容器会为每一个Web应用构建一个ServletContext用于存放所有的Servlet, Filter, Listener。Spring MVC 启动的时候主要涉及到DispatcherServlet 与 ContextLoaderListener。 关于ContextLoaderListener和DispatcherServlet ContextLoaderListener ContextLoaderListener 作为一个Listener会首先启动,创建一个WebApplicationContext用于加载除Controller等Web组件以…

2017-11-15 09:35 · java · spring

spring http session 监听 创建 失效session

一.流程概述 主要是通过扩展ServletRequestWraper来实现的。 1.SessionRepositoryFilter.doFilterInternal 2.会创建SessionRepositoryRequestWrapper,这里面的getSession(boolean)会从外部存储创建session ---sessionRepository是RedisOperationsSessionRepository ---new redissession的时候会设置一些默认属性,比如间隔时间,lastAccessTime等 这些属性关联保存在MapSession中,创建完session…

2017-11-15 09:35 · spring

spring的启动过程——spring和springMVC父子容器的原理

要想很好理解这三个上下文的关系,需要先熟悉 spring 是怎样在web容器中启动起来的。spring的启动过程其实就是其IoC容器的启动过程,对于web程序,IoC容器启动过程即是建立上下文的过程。 spring的启动过程: 首先,对于一个web应用,其部署在web容器中,web容器提供其一个全局的上下文环境,这个上下文就是ServletContext,其为后面的spring IoC容器提供宿主环境; 其次,在web.xml中会提供有contextLoaderListener。在web容器启动时,会触发容器初始化事件,此时contextLoaderListener会监听到这个事件,其con…

2017-11-15 09:29 · spring

spring session spring:session:sessions:expires 源码跟踪

/** * Saves any attributes that have been changed and updates the expiration of this * session. */ private void saveDelta() { if (this.delta.isEmpty()) { return; } String sessionId = getId(); getSessionBoundHashOperations(sessionId).putAll(this.delta); String principalSessionKey = getSessionAttrNam…

2017-11-14 09:37 · spring

spring中bean被多次实例化问题

1. 描述 spring中提供了两种主要方式实例化bean,一种为配置文件方式,另一种为注解的形式。但如果配置文件配置不合理或者注解使用不恰当,就会造成一个bean会被多次初始化的现象发生。此时会造成一种资源的浪费,严重时甚至会影响系统的性能。但此种问题有很隐蔽,如果不仔细检查,很难发现。本人是因为系统中一个定时任务被重复执行两次,经google,baidu之后才发现此问题。 以下是几种会产生此问题的配置形式。 2. 配置文件问题导致的重复初始化 2.1 问题原因 有时候,我们会将spring的配置和spring mvc的配置放在一个xml文件中,比如叫:applicationContext…

2017-11-13 09:33 · spring

spring session 退出登录 清理session

/** * Allows creating an HttpSession from a Session instance. * * @author Rob Winch * @since 1.0 */ private final class HttpSessionWrapper extends ExpiringSessionHttpSession<S> { HttpSessionWrapper(S session, ServletContext servletContext) { super(session, servletContext); } @Override //重写 session …

2017-11-10 09:26 · spring

DelegatingFilterProxy-api

为什么用DelegatingFilterProxy?窃以为,这样子做可以将DelegatingFilterProxy所代理的filter可以作为spring的bean,受到 spring的管理。而且有一点,我觉得应该注意的是,因为web应用的启动顺序是:listener->filter->servlet的,而spring容器的 启动主要是通过以下两种方式实现的:启动ContextLoaderListener或者ContextLoaderServlet的,所以,要将 将DelegatingFilterProxy所代理的filter可以作为spring的bean的话,就必须使用 启动Contex…

2017-11-09 09:51 · spring

spring父子容器与读取properties文件

读取properties文件中的内容,可以使用@Value 比如 @Value("${url}") public String url; properties文件内容是 url=xxxxxx 但是它只能在它所在容器中使用。比如spring容器中加载了properties文件,但你这个注解使用在springmvc容器(子容器)的Controller中,那么是获取不到的。 ------------------------------------------------- 关于父子容器: 子容器能够访问父容器的资源,父容器能够访问子容器的资源。 我们在web.xml中其实配置的就是父子容器org.…

2017-11-09 09:51 · spring

spring 获取bean 测试

package com.jayway.springsessionexample; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.Optional; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServ…

2017-11-09 09:51 · spring

spring session 考虑问题解答

相关问题 2.Redis容量考虑,由于spring seesion依赖redis,考虑是否和业务redis分开? Spring session 一次访问会产生3个key, 根据用户量以及程序中session.setAttribute的量来考虑是否需要跟业务redis分开 3.Redis高可用考虑,由于spring seesion依赖redis,如果redis存在单点故障,将会影响到spring session. 在redis层面做高可用,keepalived or Sentinel or redis3.0 cluster 4.浏览器不支持 cookie 怎么办? Spring session…

2017-11-09 09:50 · spring

DelegatingFilterProxy

摘要: 配置过滤器代理类,通过spring配置的bean来加载过滤器,实际并未解决我所遇到的问题,shiro集成springmvc过程中,配置shiroFiltor抛异常,无法加载shiroFilter这个bean,然而实际我在spring中已经配置好了对应 bean 在web.xml中进行配置,对所有的URL请求进行过滤,就像"击鼓传花"一样,链式处理。 配置分为两种A和B。 A:普通配置 在web.xml中增加如下内容: <filter> <filter-name>permissionFilter</filter-name> <filter-class>com.taobao.riskm.…

2017-11-08 09:48 · spring

Spring在代码中获取bean的几种方式

方法一:在初始化时保存ApplicationContext对象 方法二:通过Spring提供的utils类获取ApplicationContext对象 方法三:继承自抽象类ApplicationObjectSupport 方法四:继承自抽象类WebApplicationObjectSupport 方法五:实现接口ApplicationContextAware 方法六:通过Spring提供的ContextLoader 获取spring中bean的方式总结: 方法一:在初始化时保存ApplicationContext对象 ApplicationContext ac =newFileSystemX…

2017-11-08 09:46 · spring

Spring以及SPringmvc相关问题: ServletContext -父子容器

总结如下: 明确了Servlet规范中ServletContext的作用和意义。此外明确一个Tomcat中多个web应用,每个人web应用有唯一的一个ServletContext(全局上下文)。[例子见: 同一tomcat多个应用session问题 ] 这个ServletContext 对应JSP中内置对象javax.servlet.jsp. ServletContext(作用于 application全局级**)** 明确Spring(Spring.context包定义)上下文 ApplicationContext对象作为容器管理整个Spring生命周期.在web应用中Applicatio…

2017-11-08 09:41 · spring