springboot 项目启动时加载数据库数据

在实际项目中,很多时候会有这样的需求,在项目启动时加载数据库中公共配置数据到内存,这个过程只执行一次,之后都从内存中读取配置,在传统的 ssm 框架中,使用静态块完成这一操作,在 springboot 框架中如何加载?请看下文

 

在 springboot 框架中非常简单,只需要一个 @PostConstruct 注解即可,代码如下

package com.wt.tmp;

import com.wt.bean.Userinfo;
import com.wt.dao.UserinfoDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

@Slf4j
@Service
public class AllocationIp {

    private static CopyOnWriteArrayList<Account> accounts = new CopyOnWriteArrayList<Account>();

    @Autowired
    private UserinfoDao userinfoDao;

    @PostConstruct
    public void init(){
        log.info("项目启动中,加载用户数据");
        List<Userinfo> list = userinfoDao.findAll();
        list.forEach(v -> {
            Account account = new Account();
            account.setName(v.getId());
            account.setFlag(false);
            accounts.add(account);
        });
        log.info("项目启动中,加载用户数据完成");
    }

}

 

 

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
在Spring Boot项目启动时数据库数据缓存至Map,可以使用Spring Boot提供的ApplicationRunner或CommandLineRunner接口,在项目启动完成后执行指定的逻辑。 具体实现步骤如下: 1. 创建一个缓存,用于缓存数据库数据: ```java @Component public class DataCache { private Map<Long, Object> cache = new HashMap<>(); public void put(Long id, Object data) { cache.put(id, data); } public Object get(Long id) { return cache.get(id); } public Map<Long, Object> getAll() { return Collections.unmodifiableMap(cache); } } ``` 2. 创建一个数据加载,实现ApplicationRunner或CommandLineRunner接口,并在其实现的run方法数据库数据缓存至Map: ```java @Component public class DataLoadRunner implements ApplicationRunner { @Autowired private DataCache dataCache; @Autowired private DataSource dataSource; @Override public void run(ApplicationArguments args) throws Exception { try (Connection conn = dataSource.getConnection()) { String sql = "SELECT id, data FROM table"; try (PreparedStatement ps = conn.prepareStatement(sql); ResultSet rs = ps.executeQuery()) { while (rs.next()) { Long id = rs.getLong("id"); Object data = rs.getObject("data"); dataCache.put(id, data); } } } } } ``` 在这个例子,我们使用了Spring Boot提供的DataSource来获取数据库连接,然后使用JDBC API将数据数据库查询出来,并存储至DataCache。 3. 在Spring Boot启动添加注解@EnableScheduling,开启定时任务功能,以便在数据发生变化时,能够及时更新缓存。 ```java @SpringBootApplication @EnableScheduling public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 这样,在Spring Boot项目启动完成后,数据就会被缓存至Map,并可以通过DataCache在整个应用程序访问。如果数据发生变化,可以通过定时任务重新加载数据到缓存

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

悟世君子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值