springboot引入Application外的class类

参考:https://blog.51cto.com/longithome/2437808

 

SwaggerConfig 加载到Spring容器中的话 要怎么办呢?下面介绍两种方式

 

①:在Spring Boot Application 主类上 使用@Import 注解

@SpringBootApplication

@Import(value={SwaggerConfig.class})

②:现在我们将其改造一下,采用spring.factories 的方式去加载SwaggerConfig类,在resources目录下新建一个META-INF 的目录,然后在

新建一个spring.factories 的文件,里面的内容为:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.sg.config.SwaggerConfig

然后在把Spring Boot 启动类上的@Import注释掉,启动发现也可以把SwaggerConfig加载到Spring 容器中

 

 

在使用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操作了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值