发表博客
博客管理
Spring+SpringMVC+Redis
- 搭建Redis环境
Redis github下载地址
CSDN资源下载地址
目前官方推荐的最新稳定版本为3.2.1
下载之后直接解压得到以下目录结构
点击redis-server.exe即可启动Redis数据库
看到如下截图,Redis即启动成功 -
搭建spring环境
添加springmvc需要的依赖包<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.4.RELEASE</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.8.4</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.4</version> </dependency>
- 集成Redis
①添加Redis需要的依赖包
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.5.RELEASE</version>
</dependency>
<!-- Redis客户端 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
②配置Redis : spring-redis.xml
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 引入redis配置 -->
<context:property-placeholder location="classpath:redis.properties" ignore-unresolvable="true"/>
<!-- Redis 配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.pool.maxTotal}" />
<property name="maxIdle" value="${redis.pool.maxIdle}" />
<property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}" />
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
</bean>
<!-- redis单节点数据库连接配置 -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.ip}" />
<property name="port" value="${redis.port}" />
<!-- <property name="password" value="${redis.pass}" /> -->
<property name="poolConfig" ref="jedisPoolConfig" />
</bean>
<!-- redisTemplate配置,redisTemplate是对Jedis的对redis操作的扩展,有更多的操作,封装使操作更便捷 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
</bean>
</beans>
redis.properties
redis.pool.maxTotal=105
redis.pool.maxIdle=10
redis.pool.maxWaitMillis=5000
redis.pool.testOnBorrow=true
redis.ip=127.0.0.1
redis.port=6379
- 配置DispatcherServlet
这里采用硬编码的方式对DispatcherServlet进行配置,不依赖于web.xml
package white.yu;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import white.yu.config.RootConfig;
import white.yu.config.WebConfig;
/**
* DispatchServlet 拦截器
* @author white
*
*/
public class MainDispatcherServletInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {
/**
* 配置应用上下文
*/
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] { RootConfig.class };
}
/**
* 配置web上下文
*/
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] { WebConfig.class };
}
/**
* 配置DispatcherServlet映射路径
*/
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
应用上下文RootConfig
package white.yu.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.ImportResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@ComponentScan(basePackages = { "white.yu" }, excludeFilters = {
@Filter(type = FilterType.ANNOTATION, value = Controller.class),
@Filter(type = FilterType.ANNOTATION, value = EnableWebMvc.class) })
@ImportResource("classpath:spring/spring-*.xml") //引入redis的配置文件
public class RootConfig {
}
web上下文 WebConfig
package white.yu.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@EnableWebMvc
@ComponentScan({ "white.yu.web" })
public class WebConfig extends WebMvcConfigurerAdapter {
/**
* 配置视图解析器
*
* @return
*/
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".html");
resolver.setExposePathVariables(true);
return resolver;
}
/**
* 配置静态资源处理器
*/
@Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
super.configureDefaultServletHandling(configurer);
configurer.enable();
}
}
- 编写测试Controller
package white.yu.web;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import white.yu.cache.RedisCache;
import white.yu.entity.User;
@Controller
@RequestMapping("user")
public class UserController {
@Resource(name = "redisTemplate")
private ListOperations<String, User> listOps;
@RequestMapping("/list")
@ResponseBody
public List<User> list() {
// 获取Redis数据库中的所有user数据
// -1 表示获取数据库中的所有数据
List<User> range = listOps.range("user", 0, -1);
return range;
}
@RequestMapping("/add")
@ResponseBody
public User list(User user){
// 添加到Redis数据库
listOps.leftPush("user", user);
return user;
}
}
搭建Redis环境
Redis github下载地址
CSDN资源下载地址
目前官方推荐的最新稳定版本为3.2.1
下载之后直接解压得到以下目录结构
这里写图片描述
点击redis-server.exe即可启动Redis数据库
这里写图片描述
看到如下截图,Redis即启动成功
搭建spring环境
添加springmvc需要的依赖包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.4</version>
</dependency>
集成Redis
①添加Redis需要的依赖包
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.5.RELEASE</version>
</dependency>
<!-- Redis客户端 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
②配置Redis : spring-redis.xml
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 引入redis配置 -->
<context:property-placeholder location="classpath:redis.properties" ignore-unresolvable="true"/>
<!-- Redis 配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.pool.maxTotal}" />
<property name="maxIdle" value="${redis.pool.maxIdle}" />
<property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}" />
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
</bean>
<!-- redis单节点数据库连接配置 -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.ip}" />
<property name="port" value="${redis.port}" />
<!-- <property name="password" value="${redis.pass}" /> -->
<property name="poolConfig" ref="jedisPoolConfig" />
</bean>
<!-- redisTemplate配置,redisTemplate是对Jedis的对redis操作的扩展,有更多的操作,封装使操作更便捷 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
</bean>
</beans>
redis.properties
redis.pool.maxTotal=105
redis.pool.maxIdle=10
redis.pool.maxWaitMillis=5000
redis.pool.testOnBorrow=true
redis.ip=127.0.0.1
redis.port=6379
这里写图片描述
- 配置DispatcherServlet
这里采用硬编码的方式对DispatcherServlet进行配置,不依赖于web.xml
package white.yu;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import white.yu.config.RootConfig;
import white.yu.config.WebConfig;
/**
- DispatchServlet 拦截器
- @author white
-
*/
public class MainDispatcherServletInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {/**
- 配置应用上下文
*/
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] { RootConfig.class };
}
/**
- 配置web上下文
*/
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] { WebConfig.class };
}
/**
- 配置DispatcherServlet映射路径
*/
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
- 配置应用上下文
}
应用上下文RootConfig
package white.yu.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.ImportResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@ComponentScan(basePackages = { "white.yu" }, excludeFilters = {
@Filter(type = FilterType.ANNOTATION, value = Controller.class),
@Filter(type = FilterType.ANNOTATION, value = EnableWebMvc.class) })
@ImportResource("classpath:spring/spring-*.xml") //引入redis的配置文件
public class RootConfig {
}
web上下文 WebConfig
package white.yu.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@EnableWebMvc
@ComponentScan({ "white.yu.web" })
public class WebConfig extends WebMvcConfigurerAdapter {
/**
* 配置视图解析器
*
* @return
*/
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".html");
resolver.setExposePathVariables(true);
return resolver;
}
/**
* 配置静态资源处理器
*/
@Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
super.configureDefaultServletHandling(configurer);
configurer.enable();
}
}
编写测试Controller
package white.yu.web;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import white.yu.cache.RedisCache;
import white.yu.entity.User;
@Controller
@RequestMapping("user")
public class UserController {
@Resource(name = "redisTemplate")
private ListOperations<String, User> listOps;
@RequestMapping("/list")
@ResponseBody
public List<User> list() {
// 获取Redis数据库中的所有user数据
// -1 表示获取数据库中的所有数据
List<User> range = listOps.range("user", 0, -1);
return range;
}
@RequestMapping("/add")
@ResponseBody
public User list(User user){
// 添加到Redis数据库
listOps.leftPush("user", user);
return user;
}
}
5948
共同学习,写下你的评论
评论加载中...
作者其他优质文章