Springboot maven多模块项目中集成Jedis使用Redis

项目结构

  • 项目结构如下
  • 笔者的redis集成在demo-utils工具类模块下
    项目结构

添加maven依赖

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <version>1.5.6.RELEASE</version>
        </dependency>
        <!-- 操作redis -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>
    </dependencies>

添加配置文件

  • 在resource下创建redis.properties文件
    demo-utils结构
  • 配置文件内容如下
redis.server.host=127.0.0.1
redis.server.port=6379
redis.server.password=123321

添加RedisConfig类加载配置文件内容

package com.jie.utils.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Component
@PropertySource("classpath:redis.properties")
public class RedisConfig {
    private Logger log = LoggerFactory.getLogger(RedisConfig.class);

    @Value("${redis.server.host}")
    private String host;

    @Value("${redis.server.port}")
    private int port;

    @Value("${redis.server.password}")
    private String password;


    @Bean
    public JedisPoolConfig getRedisConfig() {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        return poolConfig;
    }




    @Bean
    public JedisPool getJedisPool() {
        JedisPoolConfig config = getRedisConfig();
        JedisPool pool = new JedisPool(config,host,port,2000,password);
        log.info("init JredisPool ...");
        return pool;
    }

}

在启动类中加注解扫描配置文件

  • 启动类中添加对com.jie.utils包的扫描
package com.jie.demoservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages = {"com.jie.serviceImpl","com.jie.utils"})
public class DemoServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoServiceApplication.class, args);
    }
}

测试

1. 在demo-iservice模块中添加接口ITestService类
  • demo-iservice模块结构如下
    demo-iservice项目结构
package com.jie.iservice;

public interface ITestService {
    String testFunction(String value);
}
2. 在demo-service模块中添加接口实现类
  • demo-service项目结构如下
     demo-service项目结构
package com.jie.serviceImpl;


import com.alibaba.dubbo.config.annotation.Service;
import com.jie.iservice.ITestService;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

@Service
public class TestServiceImpl implements ITestService {
    @Autowired
    JedisPool jedisPool;
    @Override
    public String testFunction(String value) {
        return jedisPool.getResource().set(value,value);
    }
}

3. 在demo-web模块中添加借口
  • demo-web模块结构
    demo-web模块结构
package com.jie.demoweb.control;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jie.iservice.ITestService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/test")
public class TestController {

    @Reference
    ITestService itestService;

    @GetMapping("/hello")
    public String helloWorld(@RequestParam(name = "key") String key){
        return itestService.testFunction(key);
    }
}

测试结果

测试结果
测试结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值