springboot+redis的初步整合方式

核心思路:引入spring-boot-starter-data-redis,會條件生成RedisConnectionFactory對象,然后配置一個RedisTemplate,并依賴RedisConnectionFactory對象,進而使用RedisTemplate,進行CURD的操作。
需要注意:RedisConnectionFactory對象的生成會讀Spring的配置中的redis的部分。


財去人安樂--澳門萄京酒店2017-08-05.jpg

1. 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/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>ImProject</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>ClientManager</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

<!--        可選的驅動,默認情況下是jedis-->
        <dependency>
            <groupId>io.lettuce</groupId>
            <artifactId>lettuce-core</artifactId>
        </dependency>
<!--        必選的連接池-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.5.4</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

2. applicaiton.yml

spring:
  redis:
    host: localhost
    port: 6379
    lettuce:
      pool:
        max-active: 8
#    jedis:
#      pool:
#        max-active: 8

3. 啟動配置類:

package cn.johnyu.client.manager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

@SpringBootApplication
public class ClientManagerApp {
    public static void main(String[] args) {
        SpringApplication.run(ClientManagerApp.class,args);
    }
    @Autowired
    private RedisConnectionFactory factory; //redis依賴的引入,條件生成該對象

    @Bean
    public RedisTemplate<String,Object> redisTemplate(){

       RedisTemplate redisTemplate= new RedisTemplate<>();
       redisTemplate.setConnectionFactory(factory);
       //TODO:此部分代碼,可以進行進一步的定制
        return redisTemplate;
    }
}

4. 測試類:

package cn.johnyu.client.manager;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest(classes = ClientManagerApp.class)
@RunWith(SpringRunner.class)
public class MyTest1 {
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @Test
    public void test1(){
        redisTemplate.opsForValue().set("user1","Tom123");
    }

    @Test
    public void test2(){
        Object obj=redisTemplate.opsForValue().get("user1");
        System.out.println(obj);
    }

}

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容