springboot引入redis的xml文件

听说@ImportResource(locations=)是可以的,但是却发现没用:

@SpringBootApplication
@ImportResource(value = {"classpath:spring-redis.xml"})
public class AccessingDataMysqlApplication {
    public static void main(String[] args) {
        SpringApplication.run(AccessingDataMysqlApplication.class, args);
    }
}

其实有个简单的方法,就是什么也不用加,因为boot会自动扫描resources
下spring开头的文件。所以直接放在resources下即可 。
实测成功了。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
使用SpringBoot引入Redis工具类时,首先需要在pom.xml文件中添加以下依赖: ```xml <dependencies> <!-- SpringBoot Data Redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- Redis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency> </dependencies> ``` 接下来,在SpringBoot的配置类中添加Redis的配置信息,通常是application.properties或application.yml文件。示例: ```properties # Redis配置 spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.database=0 spring.redis.password= spring.redis.timeout=30000 ``` 然后,创建一个RedisUtil工具类,用来封装Redis的常用操作方法。可以使用Jedis库对Redis进行操作。示例: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Component; @Component public class RedisUtil { @Autowired private StringRedisTemplate stringRedisTemplate; // 存储键值对 public void set(String key, String value) { stringRedisTemplate.opsForValue().set(key, value); } // 获取键对应的值 public String get(String key) { return stringRedisTemplate.opsForValue().get(key); } // 删除键值对 public void delete(String key) { stringRedisTemplate.delete(key); } // 判断键是否存在 public boolean exists(String key) { return stringRedisTemplate.hasKey(key); } } ``` 最后,可以在其他的Spring组件中使用RedisUtil来进行Redis操作。示例: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/example") public class ExampleController { @Autowired private RedisUtil redisUtil; @RequestMapping("/test") public String test() { // 存储键值对 redisUtil.set("key", "value"); // 获取键对应的值 String value = redisUtil.get("key"); return value; } } ``` 这样,就可以在SpringBoot引入Redis工具类,并进行常用的Redis操作了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值