spring整合redis
来源:互联网 发布:叶修手办淘宝 编辑:程序博客网 时间:2024/06/02 05:11
spring整合redis–流程————-
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"
));
}
}
测试成功
阅读全文
0 0
- [Spring]Redis+Spring整合
- 深入浅出Redis-Spring整合Redis
- 深入浅出Redis-Spring整合Redis
- 深入浅出Redis-Spring整合Redis
- 深入浅出Redis-Spring整合Redis
- 深入浅出Redis-Spring整合Redis
- 深入浅出Redis-Spring整合Redis
- Redis-----5、Spring整合Redis
- spring redis整合
- Spring 整合 Redis
- spring redis整合(一)
- Spring 整合 Redis 分类:
- spring整合redis
- Spring 整合 Redis
- Spring 整合 Redis
- Redis整合Spring
- spring整合redis
- Spring 整合 Redis
- centos7无法联网问题解决
- 服务管理工具-Monit
- opencv3/C++视频读取、视频写入
- 17/12/21 HelloWorld,今天起正式开启我的编程之路
- 90页第三题,创建一个 Rectangle类,添加width和lenght两个成员变量 在 Rectangle类中添加两种方法分别计算矩形的周长和面积 ,编程利用Rectangle输出一个矩形的
- spring整合redis
- 一步一步制作yaffs/yaffs2根文件系统(五)---生成yaffs/yaffs2镜像文件及下载到开发板
- charles破解
- Java 堆排序(大根堆及小根堆)
- 交叉验证 Cross-validation
- APP Inventor中如何利用GPS经纬度计算GPS距离、速度、方向
- 神经网络中的前向和后向算法
- 巧解无向图中是否存在环
- SpringBoot中自定义properties文件配置参数并带有输入提示