Spring and Redis Simple test Demo

Source: Internet
Author: User
Tags server port

1. First create a MAVEN project

File----and New--and select Maven Project under the Maven directory, such as:

Then after selecting the configuration in the popup panel, the next step is to

Create a simple project (skip archetype selection): This option means skipping the subsequent configuration options box.

Use the default Workspace location: Indicates that the project is placed under the default work area.

Once configured, next next, make the configuration as follows:

After clicking Finish, the MAVEN project was created.

This project is springredisstudy in the stump menu of the workspace workspace of your development tool.

At this point, the project will error because when the MAVEN project was first created, the Create a simple project property was selected,

So there may be no files under the WebApp project,

At this point, we need to create them ourselves.

First, add a Web-inf folder under the WebApp;

Then add a Web. XML (Deployment descriptor file) to the Web-inf folder;

Next we will do some configuration work in this file;

<?xml version= "1.0" encoding= "UTF-8"? ><web-app 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>SpringRedisStudy</display-name> <!--set the log4j configuration file location loaded by Sprng--><context-param><param-name>log4jconfiglocation</param-name ><param-value>classpath:config/property/log4j.properties</param-value></context-param> <!--spring refreshes the interval for log4j configuration file changes in milliseconds--><context-param><param-name>log4jrefreshinterval</ param-name><param-value>100000</param-value></context-param><!--load additional configuration files into the spring application context- -><listener><listener-class>org.springframework.web.context.contextloaderlistener</ listener-class></listener><!--Specify multiple spring configuration files--><context-param><param-name> Contextconfiglocation</param-name><param-value>classpath:config/xml/dispatcher-servlet.xml;classpath:config/xml/ spring-context.xml</param-value></context-param><!--Spring Character Set filter--><filter>< Filter-name>springencodingfilter</filter-name><filter-class> Org.springframework.web.filter.characterencodingfilter</filter-class><init-param><param-name >encoding</param-name><param-value>utf-8</param-value></init-param><init-param ><param-name>forceencoding</param-name><param-value>true</param-value></ Init-param></filter><filter-mapping><filter-name>springencodingfilter</filter-name> <url-pattern>/*</url-pattern></filter-mapping><!--Spring View distributor--><!--Servlet-name The name of the initialization was determined: Name-servlet.xml--><servlet><servlet-name>dispatcher</servlet-name>< Servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class><init-param><param-name>contextconfiglocation</param-name><param-value>/web-inf/ classes/conf/dispatcher-servlet.xml</param-value></init-param><load-on-startup>2</ Load-on-startup></servlet><servlet-mapping><servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern></servlet-mapping><welcome-file-list><welcome-file> Index.jsp</welcome-file></welcome-file-list></web-app>

1.2 Configuring MAVEN project Files Pom.xml

Next we build the project in detail, first configuring the MAVEN project file Pom.xml,

Because we want to build the Spring MVC project, we need to configure the relevant jar packages to be referenced in the MAVEN project;

The Pom.xml file is configured 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/xsd/maven-4.0.0.xsd" >< Modelversion>4.0.0</modelversion><groupid>springredisstudy</groupid><artifactid> springredisstudy</artifactid><version>0.0.1-snapshot</version><packaging>war</ packaging><properties><!--Spring Version number--><org.springframework.version>3.0.7.release</ Org.springframework.version><project.build.sourceencoding>utf-8</project.build.sourceencoding> <slf4j.version>1.7.5</slf4j.version><log4j.version>1.2.17</log4j.version></ properties><dependencies><!--config junit jar--><dependency><groupid>junit</ groupid><artifactid>junit</artifactid><version>4.8.2</version><scope>test< /scope></dependency><!--COnfig Redis data and client jar--><dependency><groupid>org.springframework.data</groupid>< Artifactid>spring-data-redis</artifactid><version>1.6.4.release</version></dependency ><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId> <version>2.7.3</version></dependency><dependency><groupId>org.apache.commons< /groupid><artifactid>commons-pool2</artifactid><version>2.4.2</version></ dependency><!--config need jar--><dependency><groupid>commons-lang</groupid>< artifactid>commons-lang</artifactid><version>2.6</version></dependency>< dependency><groupid>org.apache.geronimo.specs</groupid><artifactid>geronimo-servlet_3.0_ spec</artifactid><version>1.0</version></dependency><!--cofig Spring jar-->< Dependency><groupid>org.springframework</groupid><artifactid>spring-core</artifactid><version>${ Org.springframework.version}</version></dependency><dependency><groupid> org.springframework</groupid><artifactid>spring-expression</artifactid><version>${ Org.springframework.version}</version></dependency><dependency><groupid> org.springframework</groupid><artifactid>spring-beans</artifactid><version>${ Org.springframework.version}</version></dependency><dependency><groupid> org.springframework</groupid><artifactid>spring-aop</artifactid><version>${ Org.springframework.version}</version></dependency><dependency><groupid> org.springframework</groupid><artifactid>spring-context</artifactid><version>${ Org.springframework.version}</version></dependency><dependency><groupid>org.springframework</groupid><artifactid>spring-context-support</artifactid><version>${ Org.springframework.version}</version></dependency><dependency><groupid> org.springframework</groupid><artifactid>spring-tx</artifactid><version>${ Org.springframework.version}</version></dependency><dependency><groupid> org.springframework</groupid><artifactid>spring-jdbc</artifactid><version>${ Org.springframework.version}</version></dependency><dependency><groupid> org.springframework</groupid><artifactid>spring-orm</artifactid><version>${ Org.springframework.version}</version></dependency><dependency><groupid> org.springframework</groupid><artifactid>spring-oxm</artifactid><version>${ Org.springframework.version}</version></dependency><dependency><groupid> Org.springframework</groupid><artifactid>spring-web</artifactid><version>${org.springframework.version} </version></dependency><dependency><groupId>org.springframework</groupId>< Artifactid>spring-webmvc</artifactid><version>${org.springframework.version}</version> </dependency><dependency><groupId>org.springframework</groupId><artifactId> spring-webmvc-portlet</artifactid><version>${org.springframework.version}</version></ Dependency><dependency><groupid>org.springframework</groupid><artifactid> Spring-test</artifactid><version>${org.springframework.version}</version><scope>test </scope></dependency><!--Log Start--><dependency><groupid>log4j</groupid> <artifactId>log4j</artifactId><version>${log4j.version}</version></dependency> <dependency><groupid>commons-logging</groupid><artifactid>commons-logging</artifactid><version>1.2</version> </dependency><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><!--Log End--></dependencies><build><resources>< resource><directory>/src/main/resources</directory><filtering>true</filtering></ Resource></resources><plugins><plugin><artifactid>maven-war-plugin</artifactid ><configuration><version>2.5</version></configuration></plugin></plugins ></build></project>
1.3 Set into Spring configuration file

Once MAVEN is configured, we need to build the spring environment in the project;

Create a new config. spring-context.xml file in the Src/main/resources directory of the/springredisstudy project;

The purpose of this file is to configure the spring container with the following contents:

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xmlns:mvc= " Http://www.springframework.org/schema/mvc "xmlns:context=" Http://www.springframework.org/schema/context "xmlns: Util= "Http://www.springframework.org/schema/util" xsi:schemalocation= "http://www.springframework.org/schema/ Beans Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd Http://www.springframework.org/schema/co ntext http://www.springframework.org/schema/context/spring-context-3.0.xsd Http://www.springframework.org/sch EMA/MVC http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd Http://www.springframework.org/schem    A/util http://www.springframework.org/schema/util/spring-util-3.0.xsd "> <!--activating @controller mode-- <mvc:annotation-driven/> <context:annotaTion-config/> <!--scan all classes in the package to complete the function of bean creation and automatic dependency injection needs to be changed--<context:component-scan base-package= "com.t Est.* "/> <!--Introducing Redis Properties profile under the same folder--<import resource=" Redis-context.xml "/> <!--test user DAO Dependency Injection--><bean id= "Userdao" class= "Com.test.dao.impl.UserDao"/> </beans>

Create the Redis-context.xml file in the CONFIG./springredisstudy project's src/main/resources directory;

The purpose of this file is to configure Redis with the following content:

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xmlns:context=       "Http://www.springframework.org/schema/context" xsi:schemalocation= "Http://www.springframework.org/schema/beans      Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context/spring-context-3.0.xsd "><!--Introducing redis.properties profile-- <context:property-placeholderlocation= "Classpath:/config/property/redis.properties"/><!--Redis-related configuration-- ><bean id= "Poolconfig" class= "Redis.clients.jedis.JedisPoolConfig" p:maxtotal= "${redis.maxtotal}" p:maxidle= "${redis.maxidle}" p:testonborrow= "${redis.testonborrow}"/><bean id= "ConnectionFactory" class= " Org.springframework.data.redis.connection.jedis.JedisConnectionFactory "P:hostname=" ${redis.host} "p:Port= "${redis.port}" p:password= "${redis.password}" p:database= "${redis.database}" p:pool-config-ref= "PoolConfig" /><!--redis Template definition--<bean id= "Redistemplate" class= " Org.springframework.data.redis.core.RedisTemplate "p:connectionfactory-ref=" ConnectionFactory "P: keyserializer-ref= "Redistemplatekeyserializer" p:valueserializer-ref= "Redistemplatevalueserializer" P: hashkeyserializer-ref= "Redistemplatehashkeyserializer" p:hashvalueserializer-ref= " Redistemplatehashvalueserializer "/><bean id=" Redistemplatekeyserializer "class=" Org.springframework.data.redis.serializer.StringRedisSerializer "/> <bean id=" Redistemplatevalueserializer " class= "Org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/> <bean id= "  Redistemplatehashkeyserializer "class=" Org.springframework.data.redis.serializer.StringRedisSerializer "/> <bean id= "Redistemplatehashvalueserializer" class= " Org.springframework.data.redis.serializer.JdkSerializationRedIsserializer "/> </beans>    

Create the Redis-context.xml file in the new Config.property package in the Src/main/resources directory of the/springredisstudy project;

The purpose of this file is to configure Redis with the following content:

# Redis Settings
# server IP
redis.host=127.0.0.1
# Server Port
redis.port=6379
Redis.password=myredis
redis.maxtotal=600
#redis. maxactive=600 This property is not available in the new version
# Use Dbindex
Redis.database=0
# controls the maximum number of Jedis instances in a pool that have a status of idle (idle)
redis.maxidle=300
# indicates the maximum wait time when borrow (introduced) a Jedis instance, and if the wait time (in milliseconds) is exceeded, the jedisconnectionexception is thrown directly;
#redis. maxwait=5000 This property is not available in the new version
# Whether validate operations are performed in advance when a Jedis instance is borrow, and if true, the resulting Jedis instances are available
Redis.testonborrow=true

Create a new Log4j.properties file under the Src/main/resources directory of the/springredisstudy project, as follows:

#All level less than INFO'll be logged
Log4j.rootlogger=info,console
Log4j.appender.console=org.apache.log4j.consoleappender
Log4j.appender.console.immediateflush=true
Log4j.appender.console.target=system.out
Log4j.appender.console.layout=org.apache.log4j.patternlayout
Log4j.appender.stdout.layout.conversionpattern=%d{yyyy-mm-dd Hh\:mm\:ss}%m%n

Spring and Redis Simple test Demo

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.