Springboot启动加载初始化数据

本文介绍了一种在项目启动时从数据库加载系统配置参数并将其赋值给静态变量的方法,通过Spring Boot的CommandLineRunner接口实现参数的动态更新,确保应用运行时能够使用最新的配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

场景:

我们将一些系统配置参数放在数据库表中,方便随时更改。为了避免频繁读取数据库,我们仅在项目启动时加载一次参数,将这些参数赋值给静态变量。

常量类:

/**
 * @Author: BillYu
 * @Description: 常量
 * @Date: Created in 下午3:56 2018/4/28.
 */
public final class Constants {
    /**
     * 销售占比
     */
        public static Double SELLER_PERCENT = 0.12D;
}

配置类:

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

/**
 * @Author: BillYu
 * @Description:初始化赋值
 * @Date: Created in 下午5:20 2019/1/9.
 */
@Component
public class SettingConfig implements CommandLineRunner {
    @Override
    public void run(String... strings) throws Exception {
        Constants.SELLER_PERCENT=0.20D;

    }
}

controller测试:

/**
 * @Author: BillYu
 * @Description:
 * @Date: Created in 下午5:07 2018/8/21.
 */
@RequestMapping("/api/system")
@RestController
@Api(description = ")
public class TestController {

    @ApiOperation("testInit")
    @GetMapping("/init")
    public RtData init(){
        return ResponseBuilder.success(Constants.SELLER_PERCENT);
    }
}

测试结果输出:

{
  "code": 200,
  "msg": "请求成功",
  "data": 0.2
}

证明此时初始化加载已经生效。

### Spring Boot 启动初始化 Redis 连接 在 Spring Boot 中,可以通过配置文件 `application.properties` 或者 `application.yml` 来设置 Redis 的连接参数,并通过自定义逻辑实现应用启动时自动初始化 Redis 配置。以下是具体方法: #### 1. 添加依赖项 为了支持 Redis,在项目的构建工具(Maven 或 Gradle)中添加以下依赖项: ```xml <!-- Maven --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> // Gradle implementation 'org.springframework.boot:spring-boot-starter-data-redis' ``` #### 2. 配置 Redis 参数 在 `application.properties` 文件中指定 Redis 的基本连接信息: ```properties spring.redis.host=localhost spring.redis.port=6379 spring.redis.password= spring.redis.database=0 spring.redis.timeout=6000ms ``` 上述配置指定了 Redis 主机地址、端口、密码以及超时时间等基本信息[^5]。 #### 3. 自定义初始化逻辑 如果需要执行额外的初始化操作,可以创建一个监听器类来捕获 Spring Boot 应用启动完成事件 (`ApplicationReadyEvent`) 并在此处编写 Redis 初始化代码。例如: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Component; @Component public class RedisInitializer implements ApplicationListener<ContextRefreshedEvent> { @Autowired private StringRedisTemplate redisTemplate; @Override public void onApplicationEvent(ContextRefreshedEvent event) { // 执行 Redis 初始数据加载或其他操作 redisTemplate.opsForValue().set("key", "value"); System.out.println("Redis connection initialized and data loaded."); } } ``` 此代码片段展示了如何利用 `StringRedisTemplate` 对象向 Redis 数据库插入初始键值对[^6]。 #### 4. 使用 JMX 监控 Redis 性能 (可选) 如果希望监控 Redis 的性能指标,则可通过启用 JMX 功能并将其集成到应用程序中。JMX 设置如下所示: ```properties spring.jmx.enabled=true spring.jmx.default-domain=com.example.myapp ``` 这些属性允许开发者暴露管理 Bean 至 JMX 域名下以便远程管理和调试[^4]。 --- ### 注意事项 尽管提供了多个功能模块的支持,但实际开发过程中应根据项目需求合理选用组件和服务。对于简单的场景,默认配置可能已经足够;而对于复杂业务则需考虑扩展性和维护成本等因素。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值