Springboot maven多模块项目中集成Jedis使用Redis
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文件
- 配置文件内容如下
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模块结构如下
package com.jie.iservice;
public interface ITestService {
String testFunction(String value);
}
2. 在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模块结构
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);
}
}