1.pom.xml文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
<dependencies>
<!
-- spring核心包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<!
-- spring整合redis包 -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.4.1.RELEASE</version>
</dependency>
<!
-- spring整合junit包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.1.2.RELEASE</version>
<scope>test</scope>
</dependency>
<!
-- redis客户端包 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<port>9996</port>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
|
2.、applicationContext.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
<?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:aop=
"http://www.springframework.org/schema/aop"
xmlns:context=
"http://www.springframework.org/schema/context"
xmlns:jdbc=
"http://www.springframework.org/schema/jdbc"
xmlns:tx=
"http://www.springframework.org/schema/tx"
xmlns:jpa=
"http://www.springframework.org/schema/data/jpa"
xmlns:task=
"http://www.springframework.org/schema/task"
xsi:schemaLocation=
"
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"
>
<!
-- 扫描 @Server @Controller @Repository -->
<context:component-scan base-package=
"com.baidu"
/>
<!
-- 加载properties文件 -->
<context:property-placeholder location=
"classpath:config.properties"
/>
<!
-- 引入redis配置 -->
<import resource=
"applicationContext-cache.xml"
/>
<!
-- 引入mq配置 -->
<!
-- <import resource="applicationContext-mq.xml"/> -->
</beans>
|
applicationContext-cache.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
<?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:context=
"http://www.springframework.org/schema/context"
xmlns:aop=
"http://www.springframework.org/schema/aop"
xmlns:tx=
"http://www.springframework.org/schema/tx"
xmlns:p=
"http://www.springframework.org/schema/p"
xmlns:jpa=
"http://www.springframework.org/schema/data/jpa"
xmlns:jaxws=
"http://cxf.apache.org/jaxws"
xmlns:cache=
"http://www.springframework.org/schema/cache"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd"
>
<!
-- jedis 连接池配置 -->
<bean id=
"poolConfig"
class=
"redis.clients.jedis.JedisPoolConfig"
>
<property
name
=
"maxIdle"
value=
"30"
/>
<property
name
=
"maxWaitMillis"
value=
"30"
/>
<property
name
=
"testOnBorrow"
value=
"true"
/>
</bean>
<!
-- jedis 连接工厂 -->
<bean id=
"redisConnectionFactory"
class=
"org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
>
<property
name
=
"hostName"
value=
"localhost"
/>
<property
name
=
"port"
value=
"6379"
/>
<property
name
=
"poolConfig"
ref=
"poolConfig"
/>
<property
name
=
"database"
value=
"0"
></property>
</bean>
<!
-- spring data 提供 redis模板 -->
<bean id=
"redisTemplate"
class=
"org.springframework.data.redis.core.RedisTemplate"
>
<property
name
=
"connectionFactory"
ref=
"redisConnectionFactory"
/>
<!
-- 如果不指定 Serializer -->
<property
name
=
"keySerializer"
>
<bean class=
"org.springframework.data.redis.serializer.StringRedisSerializer"
/>
</property>
<property
name
=
"valueSerializer"
>
<bean class=
"org.springframework.data.redis.serializer.StringRedisSerializer"
>
</bean>
</property>
</bean>
</beans>
|
测试类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
package com.baidu.redistest;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations =
"classpath:applicationContext.xml"
)
public
class Demo {
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Test
public
void testRedis() {
// 保存
key
value
// 设置30秒失效
redisTemplate.opsForValue().
set
(
"c"
,
"北京"
, 300, TimeUnit.SECONDS);
System.
out
.println(redisTemplate.opsForValue().get(
"c"
));
}
}
|
测试成功