首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Redis系列(五)——spring整合reids

Redis系列(五)——spring整合reids

作者头像
逝兮诚
发布2019-10-30 12:52:40
4310
发布2019-10-30 12:52:40
举报
文章被收录于专栏:代码人生代码人生代码人生

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/luo4105/article/details/70821400

1.创建web项目redis_test。

2.所用jar包

3.spring配置文件application.xml代码

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
      	http://code.alibabatech.com/schema/dubbo        
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    
    <!-- 配置redis连接池 -->
    <bean id="jedisConfig" class="redis.clients.jedis.JedisPoolConfig">
    	<property name="maxTotal" value="1024"></property>  
        <property name="maxIdle" value="200"></property>  
        <property name="maxWaitMillis" value="10000"></property>  
        <property name="testOnBorrow" value="true"></property>  
    </bean>
    
    <!-- 连接工程 -->
    <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
    	<property name="hostName" value="127.0.0.1"></property>  
        <property name="port" value="6379"></property>  
        <property name="password" value="yoostar403"></property>  
        <property name="poolConfig" ref="jedisConfig"></property>  
    </bean>
    
    <!-- redis操作模板,这里采用尽量面向对象的模板 -->  
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">  
        <property name="connectionFactory" ref="connectionFactory" />  
    <!--     如果不配置Serializer,那么存储的时候只能使用String,如果用对象类型存储,那么会提示错误 can't cast to String!!!-->  
        <property name="keySerializer" ref="StringRedisSerializer" />
        <property name="valueSerializer" ref="StringRedisSerializer" />
    </bean>
    
    <!-- String序列化 -->
    <bean id="StringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" />
    
    <!-- java对象序列化 -->
    <bean id="JdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
    
    <!-- 测试自动装载 -->
    <bean id="testName" class="java.lang.String">
    	<constructor-arg value="lc" />
    </bean>
</beans>

Web.xml中加入spring配置

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>WEB-INF/application.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

3. RedisServlet.java代码

public class RedisServlet extends HttpServlet {
	private String name;	//测试单例多例
	@Autowired
	@Qualifier("redisTemplate")
	private RedisTemplate<String, String> redisTemplate;
	@Autowired
	private JedisConnectionFactory connectionFactory;
	@Autowired
	private String testName;
	/* 
	 * servlet中,要想使用@autowired,必须重写init
	 */
	@Override
	public void init(ServletConfig config) throws ServletException {
		SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,  
                config.getServletContext());
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		if("1".equals(req.getParameter("p1"))) {
			name = "p1";
		}
		Jedis jedis = connectionFactory.getShardInfo().createResource();
		System.out.println(jedis.get("name"));
		System.out.println("servlet name:" + name);
		System.out.println(redisTemplate.opsForValue().multiGet(Arrays.asList("name1", "张三", "name2", "李四", "name3", "王五")));
		System.out.println(connectionFactory);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		super.doPost(req, resp);
	}
	
	@Override
	public void destroy() {
		System.out.println("销毁");
		super.destroy();
	}
}

5.在web.xml中配置servlet。

<servlet>
  	<servlet-name>redis</servlet-name>
  	<servlet-class>com.lc.servlet.RedisServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>redis</servlet-name>
  	<url-pattern>/redis</url-pattern>
  </servlet-mapping>

现在web.xml全部如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>redis_test</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>WEB-INF/application.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <servlet>
  	<servlet-name>redis</servlet-name>
  	<servlet-class>com.lc.servlet.RedisServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>redis</servlet-name>
  	<url-pattern>/redis</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

访问:http://localhost:8080/redis_test/redis,结果如下

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-04-26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 Redis
腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档