Spring和redis简单测试demo

Posted 学海一贝

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring和redis简单测试demo相关的知识,希望对你有一定的参考价值。

1.1 首先创建一个Maven工程

File --> New --> Other,然后选择Maven目录下的Maven Project,如下图:

 

然后在弹出的面板中选择配置后,下一步即可,如图:

Create a simple project (skip archetype selection) :此选项表示跳过后续的配置选项框。

Use default Workspace location:表示将项目放置到默认的工作区域下。

配置好后,Next下一步,再进行如下图的配置:

点击Finish后,便完成了Maven工程的创建。

此时在你开发工具的Workspace工作区的树桩菜单中出现了SpringRedisStudy这个项目工程.如图:

这时,工程会报错,是因为一开始创建Maven工程时,选择的是 Create a simple project 属性,所以在webapp项目下可能会没有任何文件,如图:

这个时候就需要我们自己去创建它们。

首先在 webapp 下增加一个 WEB-INF 文件夹;然后在 WEB-INF 文件夹中增加一个 web.xml(部署描述符文件);

接下来我们就要在这个文件中进行一些配置工作。

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="2.5" 
 3         xmlns="http://java.sun.com/xml/ns/javaee"
 4         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 6                     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 7     <display-name>SpringRedisStudy</display-name>
 8     <!-- 设置由Sprng载入的Log4j配置文件位置 -->
 9     <context-param>
10         <param-name>log4jConfigLocation</param-name>
11         <param-value>classpath:config/property/log4j.properties</param-value>
12     </context-param>
13     <!-- Spring刷新Log4j配置文件变动的间隔,单位为毫秒 -->
14     <context-param>
15         <param-name>log4jRefreshInterval</param-name>
16         <param-value>100000</param-value>
17     </context-param>
18     <!-- 加载其他配置文件至Spring应用上下文中 -->
19     <listener>
20         <listener-class>
21             org.springframework.web.context.ContextLoaderListener
22         </listener-class>
23     </listener>
24     <!-- 指定多个Spring配置文件 -->
25     <context-param>
26         <param-name>contextConfigLocation</param-name>
27         <param-value>
28             classpath:config/xml/dispatcher-servlet.xml;
29             classpath:config/xml/spring-context.xml
30         </param-value>
31     </context-param>
32     <!-- Spring字符集过滤器 -->
33     <filter>
34         <filter-name>SpringEncodingFilter</filter-name>
35         <filter-class>org.springframework.web.filter.CharacterEncodingFilter
36         </filter-class>
37         <init-param>
38             <param-name>encoding</param-name>
39             <param-value>UTF-8</param-value>
40         </init-param>
41         <init-param>
42             <param-name>forceEncoding</param-name>
43             <param-value>true</param-value>
44         </init-param>
45     </filter>
46     <filter-mapping>
47         <filter-name>SpringEncodingFilter</filter-name>
48         <url-pattern>/*</url-pattern>
49     </filter-mapping>
50 
51     <!-- Spring view分发器 -->
52     <!-- servlet-name 决定了初始化的名字:name-servlet.xml -->
53     <servlet>
54         <servlet-name>dispatcher</servlet-name>
55         <servlet-class>org.springframework.web.servlet.DispatcherServlet
56         </servlet-class>
57         <init-param>
58             <param-name>contextConfigLocation</param-name>
59             <param-value>/WEB-INF/classes/conf/dispatcher-servlet.xml
60             </param-value>
61         </init-param>
62         <load-on-startup>2</load-on-startup>
63     </servlet>
64     <servlet-mapping>
65         <servlet-name>dispatcher</servlet-name>
66         <url-pattern>/</url-pattern>
67     </servlet-mapping>
68 
69     <welcome-file-list>
70         <welcome-file>index.jsp</welcome-file>
71     </welcome-file-list>
72 
73 </web-app>

 

1.2 配置maven 工程文件 pom.xml

接下来我们进行详细的项目搭建,首先配置maven 工程文件 pom.xml,

因为我们要搭建Spring MVC这个工程,所以就要将相关的jar包配置到Maven工程中进行引用管理;

pom.xml文件配置如下:

 

  1 <project xmlns="http://maven.apache.org/POM/4.0.0" 
  2         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
  4             http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5     <modelVersion>4.0.0</modelVersion>
  6     <groupId>SpringRedisStudy</groupId>
  7     <artifactId>SpringRedisStudy</artifactId>
  8     <version>0.0.1-SNAPSHOT</version>
  9     <packaging>war</packaging>
 10     <properties>
 11         <!-- spring版本号 -->
 12         <org.springframework.version>3.0.7.RELEASE</org.springframework.version>
 13         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 14         <slf4j.version>1.7.5</slf4j.version>
 15         <log4j.version>1.2.17</log4j.version>
 16     </properties>
 17 
 18     <dependencies>
 19 
 20         <!-- config junit jar -->
 21         <dependency>
 22             <groupId>junit</groupId>
 23             <artifactId>junit</artifactId>
 24             <version>4.8.2</version>
 25             <scope>test</scope>
 26         </dependency>
 27 
 28         <!-- config redis data and client jar -->
 29         <dependency>
 30             <groupId>org.springframework.data</groupId>
 31             <artifactId>spring-data-redis</artifactId>
 32             <version>1.6.4.RELEASE</version>
 33         </dependency>
 34         <dependency>
 35             <groupId>redis.clients</groupId>
 36             <artifactId>jedis</artifactId>
 37             <version>2.7.3</version>
 38         </dependency>
 39         <dependency>
 40             <groupId>org.apache.commons</groupId>
 41             <artifactId>commons-pool2</artifactId>
 42             <version>2.4.2</version>
 43         </dependency>
 44 
 45         <!-- config need jar -->
 46         <dependency>
 47             <groupId>commons-lang</groupId>
 48             <artifactId>commons-lang</artifactId>
 49             <version>2.6</version>
 50         </dependency>
 51 
 52         <dependency>
 53             <groupId>org.apache.geronimo.specs</groupId>
 54             <artifactId>geronimo-servlet_3.0_spec</artifactId>
 55             <version>1.0</version>
 56         </dependency>
 57 
 58         <!-- cofig spring jar -->
 59         <dependency>
 60             <groupId>org.springframework</groupId>
 61             <artifactId>spring-core</artifactId>
 62             <version>${org.springframework.version}</version>
 63         </dependency>
 64 
 65         <dependency>
 66             <groupId>org.springframework</groupId>
 67             <artifactId>spring-expression</artifactId>
 68             <version>${org.springframework.version}</version>
 69         </dependency>
 70 
 71         <dependency>
 72             <groupId>org.springframework</groupId>
 73             <artifactId>spring-beans</artifactId>
 74             <version>${org.springframework.version}</version>
 75         </dependency>
 76 
 77         <dependency>
 78             <groupId>org.springframework</groupId>
 79             <artifactId>spring-aop</artifactId>
 80             <version>${org.springframework.version}</version>
 81         </dependency>
 82 
 83         <dependency>
 84             <groupId>org.springframework</groupId>
 85             <artifactId>spring-context</artifactId>
 86             <version>${org.springframework.version}</version>
 87         </dependency>
 88 
 89         <dependency>
 90             <groupId>org.springframework</groupId>
 91             <artifactId>spring-context-support</artifactId>
 92             <version>${org.springframework.version}</version>
 93         </dependency>
 94 
 95         <dependency>
 96             <groupId>org.springframework</groupId>
 97             <artifactId>spring-tx</artifactId>
 98             <version>${org.springframework.version}</version>
 99         </dependency>
100 
101         <dependency>
102             <groupId>org.springframework</groupId>
103             <artifactId>spring-jdbc</artifactId>
104             <version>${org.springframework.version}</version>
105         </dependency>
106 
107         <dependency>
108             <groupId>org.springframework</groupId>
109             <artifactId>spring-orm</artifactId>
110             <version>${org.springframework.version}</version>
111         </dependency>
112 
113         <dependency>
114             <groupId>org.springframework</groupId>
115             <artifactId>spring-oxm</artifactId>
116             <version>${org.springframework.version}</version>
117         </dependency>
118 
119         <dependency>
120             <groupId>org.springframework</groupId>
121             <artifactId>spring-web</artifactId>
122             <version>${org.springframework.version}</version>
123         </dependency>
124 
125         <dependency>
126             <groupId>org.springframework</groupId>
127             <artifactId>spring-webmvc</artifactId>
128             <version>${org.springframework.version}</version>
129         </dependency>
130 
131 
132         <dependency>
133             <groupId>org.springframework</groupId>
134             <artifactId>spring-webmvc-portlet</artifactId>
135             <version>${org.springframework.version}</version>
136         </dependency>
137 
138         <dependency>
139             <groupId>org.springframework</groupId>
140             <artifactId>spring-test</artifactId>
141             <version>${org.springframework.version}</version>
142             <scope>test</scope>
143         </dependency>
144         <!-- log start -->
145         <dependency>
146             <groupId>log4j</groupId>
147             <artifactId>log4j</artifactId>
148             <version>${log4j.version}</version>
149         </dependency>
150         <dependency>
151             <groupId>commons-logging</groupId>
152             <artifactId>commons-logging</artifactId>
153             <version>1.2</以上是关于Spring和redis简单测试demo的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot超简单的测试类demo

03. 第一个spring源码调试demo

Spring的简单demo

记一次不成功的redis访问

SpringDataRedis入门Demo

带你轻松解密白盒测试(Demo详解)