假设开发一个访问redis获取缓存数据的工具包,并且在使用这个工具时,需要在配置文件中配置服务器相关设置,如host、username、password等。工具可以读取配置文件后连接redis。
一、
项目目录如下:
1、新建springboot项目:redis-cache-spring-boot-starter
2、引入springboot的starter:spring-boot-configuration-processor
其作用是,在application.yml中配置自定义属性时,会有提示功能。
3、新建RedisConfigurationProperties.java 属性类
4、新建RedisCacheUtil.java 工具类
5、新建RedisConfiguration.java 配置类
6、resources目录下新建META-INF文件夹 -》spring.factories文件,将RedisConfiguration配置到文件中:org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.demo.config.RedisConfiguration
7、打包
项目名称上右键-》run as-》maven install
二、测试
1、新建测试项目:redis-cache-test
2、引入redis-cache-spring-boot-starter的maven依赖,需要使用浏览器测试,同时引入spring-boot-starter-web
3、配置文件application.yml
4、编写测试代码
5、启动测试项目,访问:http://localhost:8080/test