
本教程旨在解决spring boot应用中从`src/main/resources`目录读取不断更新的json文件时遇到的问题。我们将深入分析`getresourceasstream`的局限性,并提供一种健壮的解决方案,通过配置外部文件路径并结合spring的`resourceloader`,实现周期性地从文件系统读取最新json数据,并将其同步至数据库,确保数据实时性与应用稳定性。
Spring Boot中动态读取更新JSON文件的实践指南
在Spring Boot应用开发中,有时我们需要周期性地读取并处理一个不断更新的外部JSON文件,并将其数据持久化到数据库。然而,直接使用Class.getResourceAsStream()方法从src/main/resources目录读取文件时,可能会遇到文件内容无法实时更新的问题。本指南将详细解释这一现象的原因,并提供一个基于Spring ResourceLoader的专业解决方案。
1. 问题解析:为什么getResourceAsStream无法读取更新?
当您将JSON文件放置在Spring Boot项目的src/main/resources目录下时,该文件在项目构建(如打包成JAR或WAR)后,会被视为应用程序的“classpath资源”。
- 开发环境: 在开发阶段,IDE通常会将src/main/resources目录直接添加到应用的classpath中,此时getResourceAsStream()可能直接读取到源文件系统中的文件。
- 生产环境: 当应用被打包成可执行JAR文件时,src/main/resources目录下的所有内容都会被嵌入到JAR包内部。getResourceAsStream()方法此时会从JAR包内部读取这些资源。一旦JAR包被创建,其内部的资源就是静态的、不可变的。即使您在文件系统上修改了原始的src/main/resources/json/file.json文件,运行中的JAR包也无法感知到这些外部修改,它始终读取的是打包时包含的那个版本。
因此,如果您的JSON文件是需要被外部进程或应用自身周期性更新的,并期望应用能读取到这些更新,那么将其作为classpath资源处理是不可行的。我们需要一种直接访问文件系统的方式。
2. 解决方案:从文件系统读取动态文件
为了解决上述问题,我们需要让Spring Boot应用直接从文件系统而不是classpath中读取文件。Spring框架提供了强大的ResourceLoader接口,能够以统一的方式加载各种类型的资源,包括文件系统资源、classpath资源、URL资源等。
核心思路如下:
- 配置外部文件路径: 将JSON文件的路径定义为可配置的属性,最好是文件系统路径。
- 使用ResourceLoader: 注入ResourceLoader,并结合配置的文件路径来获取Resource对象。
- 获取输入流: 从Resource对象获取输入流,进而解析JSON数据。
3. 实施步骤
我们将通过一个具体的示例来演示如何实现这一功能。
3.1 步骤一:配置JSON文件路径
在src/main/resources/application.properties或application.yml中定义JSON文件的路径。为了在开发环境中方便测试,我们可以将其指向项目内部的src/main/resources目录,但在生产环境中,建议指向一个外部的、独立的目录。
src/main/resources/application.properties
# 定义JSON文件的文件系统路径 # 在开发环境中,可以指向项目内部的resources目录 # 在生产环境中,应指向外部的绝对路径,例如:file:/opt/app/data/file.json app.data.json-file-path=file:./src/main/resources/json/file.json
这里使用file:前缀明确指示ResourceLoader这是一个文件系统资源。./表示当前工作目录。
3.2 步骤二:创建数据模型和仓库
首先,确保您的数据模型(Master)和JPA仓库(MasterRepository)已正确定义。
src/main/java/com/example/demo/model/Master.java
package com.example.demo.model;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Master {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String value;
// 根据您的JSON结构添加其他字段
}src/main/java/com/example/demo/Repository/MasterRepository.java
package com.example.demo.Repository; import com.example.demo.model.Master; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; @Repository public interface MasterRepository extends CrudRepository{ }
3.3 步骤三:创建服务层处理数据持久化
MasterService负责处理Master对象的业务逻辑,特别是保存操作。
src/main/java/com/example/demo/Services/MasterService.java
package com.example.demo.Services;
import com.example.demo.Repository.MasterRepository;
import com.example.demo.model.Master;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; // 导入Transactional
import java.util.List;
@Service
public class MasterService {
private final MasterRepository masterRepository;
// 推荐使用构造器注入
public MasterService(MasterRepository masterRepository) {
this.masterRepository = masterRepository;
}
public Iterable list() {
return masterRepository.findAll();
}
@Transactional // 确保批量保存操作在一个事务中
public Iterable save(List masters) {
// 实际应用中,您可能需要处理更新或删除旧数据,以避免重复或过时数据
// 例如:先清空表或根据业务ID进行更新
// masterRepository.deleteAll(); // 如果是全量更新
return masterRepository.saveAll(masters);
}
@Transactional
public Master save(Master master){
return masterRepository.save(master);
}
} 注意: 在save(List
3.4 步骤四:创建专门的服务处理文件读取与数据同步
将文件读取和数据同步的逻辑封装到一个独立的Spring Service中,并使用@Scheduled注解实现周期性执行。
src/main/java/com/example/demo/Services/JsonFileProcessor.java
package com.example.demo.Services;
import com.example.demo.model.Master;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
@Service
public class JsonFileProcessor {
private final MasterService masterService;
private final ObjectMapper objectMapper;
private final ResourceLoader resourceLoader;
private final String jsonFilePath;
// 使用构造器注入所有依赖和配置值
public JsonFileProcessor(MasterService masterService,
ObjectMapper objectMapper,
ResourceLoader resourceLoader,
@Value("${app.data.json-file-path}") String jsonFilePath) {
this.masterService = masterService;
this.objectMapper = objectMapper;
this.resourceLoader = resourceLoader;
this.jsonFilePath = jsonFilePath;
}
@Scheduled(fixedRate = 90000) // 每90秒执行一次
public void processAndUpdateData() {
System.out.println("Scheduled task started: Reading JSON file from " + jsonFilePath);
try {
// 使用ResourceLoader获取资源
Resource resource = resourceLoader.getResource(jsonFilePath);
// 检查资源是否存在且可读
if (!resource.exists()) {
System.err.println("Error: JSON file not found at: " + jsonFilePath);
return;
}
if (!resource.isReadable()) {
System.err.println("Error: JSON file not readable at: " + jsonFilePath);
return;
}
// 使用try-with-resources确保InputStream被正确关闭
try (InputStream inputStream = resource.getInputStream()) {
TypeReference> typeReference = new TypeReference>() {};
List masters = objectMapper.readValue(inputStream, typeReference);
if (masters != null && !masters.isEmpty()) {
System.out.println("Read " + masters.size() + " records from JSON. First item: " + masters.get(0));
masterService.save(masters);
System.out.println("Saved " + masters.size() + " records to database.");
} else {
System.out.println("No records found in JSON file or file is empty.");
}
}
} catch (IOException e) {
System.err.println("Error processing JSON file '" + jsonFilePath +










