pom主要配置
<properties> <spring.version>5.0.3.RELEASE</spring.version> <commons-lang.version>3.2.1</commons-lang.version> <slf4j.version>1.6.1</slf4j.version> <jackson.version>2.9.4</jackson.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc5</artifactId> <version>11.2.0.3</version> </dependency> <!-- spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-instrument</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>${commons-lang.version}</version> </dependency> <!-- slf4j --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.session/spring-session-data-redis --> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> <version>2.0.1.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.4.2</version> </dependency> <!-- https://mvnrepository.com/artifact/redis.clients/jedis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> </dependencies>
spring-servlet.xml配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.lei"/> <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <property name="viewResolvers"> <list> <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/pages/"/> <property name="suffix" value=".jsp"/> </bean> </list> </property> </bean> <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> <value>application/json;charset=UTF-8</value> </list> </property> <property name="objectMapper" ref="objectMapper"/> </bean> </mvc:message-converters> </mvc:annotation-driven> <bean id="objectMapper" class="com.fasterxml.jackson.databind.ObjectMapper"> <property name="dateFormat"> <bean class="java.text.SimpleDateFormat"> <constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss"/> </bean> </property> <property name="propertyNamingStrategy"> <bean class="com.fasterxml.jackson.databind.PropertyNamingStrategy.UpperCamelCaseStrategy"> </bean> </property> </bean> <context:property-placeholder location="classpath:redis.properties" file-encoding="UTF-8"/> <bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"> <property name="maxInactiveIntervalInSeconds" value="60"/> </bean> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="${redis.hostName.cloudq}"/> <property name="port" value="${redis.port.cloudq}"/> <property name="timeout" value="${redis.timeout.cloudq}"/> <property name="database" value="${redis.dbIndex}"/> <property name="usePool" value="${redis.usePool}"/> <property name="password" value="${redis.password.cloudq}" /> <!--可以通过构造注入或者Set注入两种方式--> <property name="poolConfig" ref="jedisPoolConfig"/> </bean> <!--配置 jedis pool--> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <!-- 最大连接数 --> <property name="maxTotal" value="${redis.pool.maxTotal}"/> <!-- 最大空闲时间 --> <property name="maxIdle" value="${redis.pool.maxIdle}"/> <!-- 每次最大连接数 --> <property name="numTestsPerEvictionRun" value="${redis.pool.numTestsPerEvictionRun}"/> <!-- 释放扫描的扫描间隔 --> <property name="timeBetweenEvictionRunsMillis" value="${redis.pool.timeBetweenEvictionRunsMillis}"/> <!-- 连接的最小空闲时间 --> <property name="minEvictableIdleTimeMillis" value="${redis.pool.minEvictableIdleTimeMillis}"/> <!-- 连接控歘按时间多久后释放,当空闲时间>该值且空闲连接>最大空闲连接数时直接释放 --> <property name="softMinEvictableIdleTimeMillis" value="${redis.pool.softMinEvictableIdleTimeMillis}"/> <!-- 获得链接时的最大等待毫秒数,小于0:阻塞不确定时间,默认-1 --> <property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}"/> <!-- 在获得链接的时候检查有效性,默认false --> <property name="testOnBorrow" value="${redis.pool.testOnBorrow}"/> <!-- 在空闲时检查有效性,默认false --> <property name="testWhileIdle" value="${redis.pool.testWhileIdle}"/> <!-- 连接耗尽时是否阻塞,false报异常,true阻塞超时 默认:true--> <property name="blockWhenExhausted" value="${redis.pool.blockWhenExhausted}"/> </bean> <bean class="org.springframework.session.web.http.SessionEventHttpSessionListenerAdapter"> <constructor-arg > <list> <bean class="com.lei.SessionListener" /> </list> </constructor-arg> </bean> </beans>
redis.properties配置
#JedisPoolConfig的参数 #最大连接数 redis.pool.maxTotal=30 #最大空闲时间 redis.pool.maxIdle=10 #每次最大连接数 redis.pool.numTestsPerEvictionRun=1024 #释放扫描的扫描间隔 redis.pool.timeBetweenEvictionRunsMillis=30000 #连接的最小空闲时间 redis.pool.minEvictableIdleTimeMillis=1800000 #连接控歘按时间多久后释放,当空闲时间>该值且空闲连接>最大空闲连接数时直接释放 redis.pool.softMinEvictableIdleTimeMillis=10000 #获得链接时的最大等待毫秒数,小于0:阻塞不确定时间,默认-1 redis.pool.maxWaitMillis=1500 #在获得链接的时候检查有效性,默认false redis.pool.testOnBorrow=true #在空闲时检查有效性,默认false redis.pool.testWhileIdle=true #连接耗尽时是否阻塞,false报异常,true阻塞超时,默认true redis.pool.blockWhenExhausted=false #JedisConnectionFactory的参数 #主机地址,默认:localhost #redis.hostName=192.168.30.249 redis.hostName.cloudq=192.168.30.249 #主机端口,默认:6379 redis.port.cloudq=6379 #超时时间,默认:2000 redis.timeout.cloudq=3000 #密码 redis.password.cloudq=mimatext #是否使用连接池,默认true redis.usePool=true #使用数据库的索引,0-15之间的数字,默认:0 redis.dbIndex=0
web.xml配置
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name>springmvc</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath*:spring-servlet.xml </param-value> </context-param> <servlet> <servlet-name>springMvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value> classpath*:spring-servlet.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <filter-name>springSessionRepositoryFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>springSessionRepositoryFilter</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>ERROR</dispatcher> </filter-mapping> <filter> <description>字符集过滤器</description> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <description>字符集编码</description> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <description>Introspector缓存清除监听器</description> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <listener> <description>request监听器</description> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener> <listener> <listener-class>com.lei.SessionListener</listener-class> </listener> <session-config> <session-timeout>1</session-timeout> </session-config> </web-app>
SessionListener
package com.lei; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; /** * Created by zhanglei on 2018/2/7. */ public class SessionListener implements HttpSessionListener { private static final Log log = LogFactory.getLog(TestController.class); @Override public void sessionCreated(HttpSessionEvent se) { log.info(se.getSession().getId()+"创建了"); } @Override public void sessionDestroyed(HttpSessionEvent se) { log.info(se.getSession().getId()+"销毁"); } }