版权声明:本文为博主原创文章,遵循 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>