Http://www.open-open.com/lib/view/open1436322883958.html
One:
Create a new Maven WebApp project and join the dependencies of spring session
Pom.xml as follows:
<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" ><modelversion >4.0.0</modelversion><groupid>com.lala</groupid><artifactid>leshop</artifactid ><packaging>war</packaging><version>0.0.1-snapshot</version><name>taobao Maven webapp</name><url>http://maven.apache.org</url><dependencies><dependency>< Groupid>junit</groupid><artifactid>junit</artifactid><version>4.10</version> <scope>test</scope></dependency><dependency><groupid>javax.servlet</groupid ><artifactid>jsp-api</artifactid><version>2.0</version><scope>provided</ Scope></dependency><dependency><groupid>javax.servlet</groupid><artifactid> javax.servlet-api≪/artifactid><version>3.0.1</version><scope>provided</scope></dependency> <dependency><groupId>org.springframework.session</groupId><artifactId> Spring-session-data-redis</artifactid><version>1.0.1.release</version></dependency> <dependency><groupid>org.springframework</groupid><artifactid>spring-web</ Artifactid><version>4.1.6.release</version></dependency><dependency><groupid> Org.springframework.session</groupid><artifactid>spring-session</artifactid><version> 1.0.1.release</version></dependency><dependency><groupid>com.orange.redis-embedded< /groupid><artifactid>embedded-redis</artifactid><version>0.6</version></ dependency></dependencies><build><finalname>leshop</finalname><plugins>< Plugin><groupid>org.apache.maven.plugins</groupid><artifactid>maven-compiler-plugin</artifactid><version>3.3</version>< configuration><source>1.8</source><target>1.8</target><verbose>true</ Verbose></configuration></plugin></plugins></build></project>
Two:
Configuring the Redis Environment
1: Install Redis, start
2:
Package Com.lala.config;import Org.springframework.context.annotation.bean;import Org.springframework.data.redis.connection.jedis.jedisconnectionfactory;import org.springframework.session.data.redis.config.annotation.web.http.enableredishttpsession;@ Enableredishttpsessionpublic class Sessionconfig {@Beanpublic jedisconnectionfactory connectionfactory () { Jedisconnectionfactory connection = new Jedisconnectionfactory (); Connection.setport (6379); Connection.sethostname (" 192.168.1.106 "); return connection;}}
Package Com.lala.config;import Org.springframework.session.web.context.AbstractHttpSessionApplicationInitializer ;p ublic class Sessioninitializer extends Abstracthttpsessionapplicationinitializer {public Sessioninitializer () { Super (Sessionconfig.class);}}
Three:
Finally, inside a servlet or action
The Integer uid = integer.valueof (Req.getparameter ("UID")), Req.getsession (). SetAttribute ("User", new User (UID, "Zhang San", " This is Redis's "));
After execution, the session was found to be stored in Redis.
This approach does not need to rely on specific containers. Can be used in any container.
Unlike the online approach, using other jars, you also need to configure Tomcat. This practice does not support other Web server
Spring Session Redis