Springmvc+Redis應用例項
第一步: 建立maven專案:
例項pom.xml內容如下
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"><span style="color: #0000ff;"><</span><span style="color: #800000;">modelVersion</span><span style="color: #0000ff;">></span>4.0.0<span style="color: #0000ff;"></</span><span style="color: #800000;">modelVersion</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">packaging</span><span style="color: #0000ff;">></span>war<span style="color: #0000ff;"></</span><span style="color: #800000;">packaging</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">name</span><span style="color: #0000ff;">></span>orm-bae<span style="color: #0000ff;"></</span><span style="color: #800000;">name</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">></span>com.orm<span style="color: #0000ff;"></</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">></span>orm-bae<span style="color: #0000ff;"></</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">version</span><span style="color: #0000ff;">></span>0.0.1-SNAPSHOT<span style="color: #0000ff;"></</span><span style="color: #800000;">version</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">properties</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">org.springframework.version</span><span style="color: #0000ff;">></span>3.0.5.RELEASE<span style="color: #0000ff;"></</span><span style="color: #800000;">org.springframework.version</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">project.build.sourceEncoding</span><span style="color: #0000ff;">></span>UTF-8<span style="color: #0000ff;"></</span><span style="color: #800000;">project.build.sourceEncoding</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">properties</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">dependencies</span><span style="color: #0000ff;">></span> <span style="color: #008000;"><!--</span><span style="color: #008000;"> config junit jar </span><span style="color: #008000;">--></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">dependency</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">></span>junit<span style="color: #0000ff;"></</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">></span>junit<span style="color: #0000ff;"></</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">version</span><span style="color: #0000ff;">></span>4.8.2<span style="color: #0000ff;"></</span><span style="color: #800000;">version</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">scope</span><span style="color: #0000ff;">></span>test<span style="color: #0000ff;"></</span><span style="color: #800000;">scope</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">dependency</span><span style="color: #0000ff;">></span> <span style="color: #008000;"><!--</span><span style="color: #008000;"> config redis data and client jar</span><span style="color: #008000;">--></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">dependency</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">></span>org.springframework.data<span style="color: #0000ff;"></</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">></span>spring-data-redis<span style="color: #0000ff;"></</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">version</span><span style="color: #0000ff;">></span>1.0.2.RELEASE<span style="color: #0000ff;"></</span><span style="color: #800000;">version</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">dependency</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">dependency</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">></span>redis.clients<span style="color: #0000ff;"></</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">></span>jedis<span style="color: #0000ff;"></</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">version</span><span style="color: #0000ff;">></span>2.1.0<span style="color: #0000ff;"></</span><span style="color: #800000;">version</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">dependency</span><span style="color: #0000ff;">></span> <span style="color: #008000;"><!--</span><span style="color: #008000;"> config need jar </span><span style="color: #008000;">--></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">dependency</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">></span>commons-lang<span style="color: #0000ff;"></</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">></span>commons-lang<span style="color: #0000ff;"></</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">version</span><span style="color: #0000ff;">></span>2.6<span style="color: #0000ff;"></</span><span style="color: #800000;">version</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">dependency</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">dependency</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">></span>org.apache.geronimo.specs<span style="color: #0000ff;"></</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">></span>geronimo-servlet_3.0_spec<span style="color: #0000ff;"></</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">version</span><span style="color: #0000ff;">></span>1.0<span style="color: #0000ff;"></</span><span style="color: #800000;">version</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">dependency</span><span style="color: #0000ff;">></span> <span style="color: #008000;"><!--</span><span style="color: #008000;"> cofig spring jar </span><span style="color: #008000;">--></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">dependency</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">></span>org.springframework<span style="color: #0000ff;"></</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">></span>spring-core<span style="color: #0000ff;"></</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">version</span><span style="color: #0000ff;">></span>${org.springframework.version}<span style="color: #0000ff;"></</span><span style="color: #800000;">version</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">dependency</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">dependency</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">></span>org.springframework<span style="color: #0000ff;"></</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">></span>spring-expression<span style="color: #0000ff;"></</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">version</span><span style="color: #0000ff;">></span>${org.springframework.version}<span style="color: #0000ff;"></</span><span style="color: #800000;">version</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">dependency</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">dependency</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">></span>org.springframework<span style="color: #0000ff;"></</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">></span>spring-beans<span style="color: #0000ff;"></</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">version</span><span style="color: #0000ff;">></span>${org.springframework.version}<span style="color: #0000ff;"></</span><span style="color: #800000;">version</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">dependency</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">dependency</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">></span>org.springframework<span style="color: #0000ff;"></</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">></span>spring-aop<span style="color: #0000ff;"></</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">version</span><span style="color: #0000ff;">></span>${org.springframework.version}<span style="color: #0000ff;"></</span><span style="color: #800000;">version</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">dependency</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">dependency</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">></span>org.springframework<span style="color: #0000ff;"></</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">></span>spring-context<span style="color: #0000ff;"></</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">version</span><span style="color: #0000ff;">></span>${org.springframework.version}<span style="color: #0000ff;"></</span><span style="color: #800000;">version</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">dependency</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">dependency</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">></span>org.springframework<span style="color: #0000ff;"></</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">></span>spring-context-support<span style="color: #0000ff;"></</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">version</span><span style="color: #0000ff;">></span>${org.springframework.version}<span style="color: #0000ff;"></</span><span style="color: #800000;">version</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">dependency</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">dependency</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">></span>org.springframework<span style="color: #0000ff;"></</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">></span>spring-tx<span style="color: #0000ff;"></</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">version</span><span style="color: #0000ff;">></span>${org.springframework.version}<span style="color: #0000ff;"></</span><span style="color: #800000;">version</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">dependency</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">dependency</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">></span>org.springframework<span style="color: #0000ff;"></</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">></span>spring-jdbc<span style="color: #0000ff;"></</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">version</span><span style="color: #0000ff;">></span>${org.springframework.version}<span style="color: #0000ff;"></</span><span style="color: #800000;">version</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">dependency</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">dependency</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">></span>org.springframework<span style="color: #0000ff;"></</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">></span>spring-orm<span style="color: #0000ff;"></</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">version</span><span style="color: #0000ff;">></span>${org.springframework.version}<span style="color: #0000ff;"></</span><span style="color: #800000;">version</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">dependency</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">dependency</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">></span>org.springframework<span style="color: #0000ff;"></</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">></span>spring-oxm<span style="color: #0000ff;"></</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">version</span><span style="color: #0000ff;">></span>${org.springframework.version}<span style="color: #0000ff;"></</span><span style="color: #800000;">version</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">dependency</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">dependency</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">></span>org.springframework<span style="color: #0000ff;"></</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">></span>spring-web<span style="color: #0000ff;"></</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">version</span><span style="color: #0000ff;">></span>${org.springframework.version}<span style="color: #0000ff;"></</span><span style="color: #800000;">version</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">dependency</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">dependency</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">></span>org.springframework<span style="color: #0000ff;"></</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">></span>spring-webmvc<span style="color: #0000ff;"></</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">version</span><span style="color: #0000ff;">></span>${org.springframework.version}<span style="color: #0000ff;"></</span><span style="color: #800000;">version</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">dependency</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">dependency</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">></span>org.springframework<span style="color: #0000ff;"></</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">></span>spring-webmvc-portlet<span style="color: #0000ff;"></</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">version</span><span style="color: #0000ff;">></span>${org.springframework.version}<span style="color: #0000ff;"></</span><span style="color: #800000;">version</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">dependency</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">dependency</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">></span>org.springframework<span style="color: #0000ff;"></</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">></span>spring-test<span style="color: #0000ff;"></</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">version</span><span style="color: #0000ff;">></span>${org.springframework.version}<span style="color: #0000ff;"></</span><span style="color: #800000;">version</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">scope</span><span style="color: #0000ff;">></span>test<span style="color: #0000ff;"></</span><span style="color: #800000;">scope</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">dependency</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">dependencies</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">build</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">resources</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">resource</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">directory</span><span style="color: #0000ff;">></span>/src/main/resources<span style="color: #0000ff;"></</span><span style="color: #800000;">directory</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">filtering</span><span style="color: #0000ff;">></span>true<span style="color: #0000ff;"></</span><span style="color: #800000;">filtering</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">resource</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">resources</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">build</span><span style="color: #0000ff;">></span>
</project>
第二步:配置檔案整合
Web.xml檔案配置
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><span style="color: #0000ff;"><</span><span style="color: #800000;">listener</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">listener-class</span><span style="color: #0000ff;">></span>org.springframework.web.context.ContextLoaderListener<span style="color: #0000ff;"></</span><span style="color: #800000;">listener-class</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">listener</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">context-param</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">param-name</span><span style="color: #0000ff;">></span>contextConfigLocation<span style="color: #0000ff;"></</span><span style="color: #800000;">param-name</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">param-value</span><span style="color: #0000ff;">></span>/WEB-INF/context/spring-context.xml<span style="color: #0000ff;"></</span><span style="color: #800000;">param-value</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">context-param</span><span style="color: #0000ff;">></span> <span style="color: #008000;"><!--</span><span style="color: #008000;"> Spring字符集過濾器 </span><span style="color: #008000;">--></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">filter</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">filter-name</span><span style="color: #0000ff;">></span>SpringEncodingFilter<span style="color: #0000ff;"></</span><span style="color: #800000;">filter-name</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">filter-class</span><span style="color: #0000ff;">></span>org.springframework.web.filter.CharacterEncodingFilter<span style="color: #0000ff;"></</span><span style="color: #800000;">filter-class</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">init-param</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">param-name</span><span style="color: #0000ff;">></span>encoding<span style="color: #0000ff;"></</span><span style="color: #800000;">param-name</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">param-value</span><span style="color: #0000ff;">></span>UTF-8<span style="color: #0000ff;"></</span><span style="color: #800000;">param-value</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">init-param</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">init-param</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">param-name</span><span style="color: #0000ff;">></span>forceEncoding<span style="color: #0000ff;"></</span><span style="color: #800000;">param-name</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">param-value</span><span style="color: #0000ff;">></span>true<span style="color: #0000ff;"></</span><span style="color: #800000;">param-value</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">init-param</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">filter</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">filter-mapping</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">filter-name</span><span style="color: #0000ff;">></span>SpringEncodingFilter<span style="color: #0000ff;"></</span><span style="color: #800000;">filter-name</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">url-pattern</span><span style="color: #0000ff;">></span>/*<span style="color: #0000ff;"></</span><span style="color: #800000;">url-pattern</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">filter-mapping</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">servlet</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">servlet-name</span><span style="color: #0000ff;">></span>mvc<span style="color: #0000ff;"></</span><span style="color: #800000;">servlet-name</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">servlet-class</span><span style="color: #0000ff;">></span>org.springframework.web.servlet.DispatcherServlet<span style="color: #0000ff;"></</span><span style="color: #800000;">servlet-class</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">init-param</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">param-name</span><span style="color: #0000ff;">></span>contextConfigLocation<span style="color: #0000ff;"></</span><span style="color: #800000;">param-name</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">param-value</span><span style="color: #0000ff;">></span>/WEB-INF/context/spring-mvc.xml<span style="color: #0000ff;"></</span><span style="color: #800000;">param-value</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">init-param</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">load-on-startup</span><span style="color: #0000ff;">></span>2<span style="color: #0000ff;"></</span><span style="color: #800000;">load-on-startup</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">servlet</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">servlet-mapping</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">servlet-name</span><span style="color: #0000ff;">></span>mvc<span style="color: #0000ff;"></</span><span style="color: #800000;">servlet-name</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">url-pattern</span><span style="color: #0000ff;">></span>/<span style="color: #0000ff;"></</span><span style="color: #800000;">url-pattern</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">servlet-mapping</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">welcome-file-list</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">welcome-file</span><span style="color: #0000ff;">></span>index.jsp<span style="color: #0000ff;"></</span><span style="color: #800000;">welcome-file</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">welcome-file-list</span><span style="color: #0000ff;">></span>
</web-app>
spring-context.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:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"><span style="color: #008000;"><!--</span><span style="color: #008000;"> 啟用@Controller模式 </span><span style="color: #008000;">--></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">mvc:annotation-driven </span><span style="color: #0000ff;">/></span> <span style="color: #008000;"><!--</span><span style="color: #008000;"> 對包中的所有類進行掃描,以完成Bean建立和自動依賴注入的功能 需要更改 </span><span style="color: #008000;">--></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">context:component-scan </span><span style="color: #ff0000;">base-package</span><span style="color: #0000ff;">="com.pudp.bae.*"</span> <span style="color: #0000ff;">/></span> <span style="color: #008000;"><!--</span><span style="color: #008000;"> 引入同文件夾下的redis屬性配置檔案 </span><span style="color: #008000;">--></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">import </span><span style="color: #ff0000;">resource</span><span style="color: #0000ff;">="redis-context.xml"</span><span style="color: #0000ff;">/></span>
</beans>
redis-context.xml內容
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd "><span style="color: #008000;"><!--</span><span style="color: #008000;"> scanner redis properties </span><span style="color: #008000;">--></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">context:property-placeholder </span><span style="color: #ff0000;">location</span><span style="color: #0000ff;">="/WEB-INF/property/redis.properties"</span> <span style="color: #0000ff;">/></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">bean </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="poolConfig"</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">="redis.clients.jedis.JedisPoolConfig"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">property </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="maxIdle"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="${redis.maxIdle}"</span> <span style="color: #0000ff;">/></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">property </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="maxActive"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="${redis.maxActive}"</span> <span style="color: #0000ff;">/></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">property </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="maxWait"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="${redis.maxWait}"</span> <span style="color: #0000ff;">/></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">property </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="testOnBorrow"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="${redis.testOnBorrow}"</span> <span style="color: #0000ff;">/></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">bean</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">bean </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="connectionFactory"</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"</span><span style="color: #ff0000;"> p:host-name</span><span style="color: #0000ff;">="${redis.host}"</span><span style="color: #ff0000;"> p:port</span><span style="color: #0000ff;">="${redis.port}"</span><span style="color: #ff0000;"> p:password</span><span style="color: #0000ff;">="${redis.pass}"</span><span style="color: #ff0000;"> p:pool-config-ref</span><span style="color: #0000ff;">="poolConfig"</span><span style="color: #0000ff;">/></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">bean </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="redisTemplate"</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">="org.springframework.data.redis.core.StringRedisTemplate"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">property </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="connectionFactory"</span><span style="color: #ff0000;"> ref</span><span style="color: #0000ff;">="connectionFactory"</span> <span style="color: #0000ff;">/></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">bean</span><span style="color: #0000ff;">></span>
</beans>
Redis.properties檔案內容
# Redis settings
#redis.host=192.168.20.101
#redis.port=6380
#redis.pass=foobared
redis.host=127.0.0.1
redis.port=6379
redis.pass=
redis.maxIdle=300
redis.maxActive=600
redis.maxWait=1000
redis.testOnBorrow=true
第三步:Springmvc對Redis的支援與例項應用.
例項物件
package com.pudp.bae.model;
import com.pudp.bae.base.BaseModel;
public class Member extends BaseModel{
/**
*
*/
private static final long serialVersionUID = -1959528436584592183L;
private String id;
private String nickname;
public Member(){}
public Member(String id, String nickname){
this.setId(id);
this.setNickname(nickname);
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
}
View CodeRedis物件持久化操作
package com.pudp.bae.base;
import java.io.Serializable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
public abstract class RedisGeneratorDao<K extends Serializable, V extends Serializable> {
@Autowired
</span><span style="color: #0000ff;">protected</span> RedisTemplate<K,V><span style="color: #000000;"> redisTemplate ;
</span><span style="color: #008000;">/**</span><span style="color: #008000;">
* 設定redisTemplate
* </span><span style="color: #808080;">@param</span><span style="color: #008000;"> redisTemplate the redisTemplate to set
</span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> setRedisTemplate(RedisTemplate<K, V><span style="color: #000000;"> redisTemplate) {
</span><span style="color: #0000ff;">this</span>.redisTemplate =<span style="color: #000000;"> redisTemplate;
}
</span><span style="color: #008000;">/**</span><span style="color: #008000;">
* 獲取 RedisSerializer
* <br>------------------------------<br>
</span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">protected</span> RedisSerializer<String><span style="color: #000000;"> getRedisSerializer() {
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> redisTemplate.getStringSerializer();
}
}
package com.pudp.bae.base;
import java.io.Serializable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
public abstract class RedisGeneratorDao<K extends Serializable, V extends Serializable> {
@Autowired
</span><span style="color: #0000ff;">protected</span> RedisTemplate<K,V><span style="color: #000000;"> redisTemplate ;
</span><span style="color: #008000;">/**</span><span style="color: #008000;">
* 設定redisTemplate
* </span><span style="color: #808080;">@param</span><span style="color: #008000;"> redisTemplate the redisTemplate to set
</span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> setRedisTemplate(RedisTemplate<K, V><span style="color: #000000;"> redisTemplate) {
</span><span style="color: #0000ff;">this</span>.redisTemplate =<span style="color: #000000;"> redisTemplate;
}
</span><span style="color: #008000;">/**</span><span style="color: #008000;">
* 獲取 RedisSerializer
* <br>------------------------------<br&