springboot 打jar 包部署时 读取外部配置文件

案例:本文主要描述linux系统执行jar包读取jar包同级目录的外部配置文件
方法一:相对路径设置配置文件
(1)在jar包同级目录创建配置文件conf.properties并写入配置数据:

confData=data
1
(2)开始写入自动化测试代码

//java项目www fhadmin org
public class Test{
    public String getData() throws IOException {
        //读取配置文件
        Properties properties = new Properties();
        File file = new File("conf.properties");
        FileInputStream fis = new FileInputStream(file);
        properties.load(fis);
        fis.close();
        //获取配置文件数据
        String confData = properties.getProperty("confData");
        System.out.println(confData);
    }
}


(3)执行jar包

java -jar jarNanexxx


方法二:绝对路径设置配置文件
解决问题:使用相对路径的方法在jar包同级目录手动执行jar包时没有问题,但使用linux系统的crontab文件定时调度时报错,原因:因为我们手动执行某个脚本时,是在当前shell环境下进行的,程序能找到环境变量;而系统自动执行任务调度时,除了默认的环境,是不会加载任何其他环境变量的。因此就需要在crontab文件中指定任务运行所需的所有环境变量,或者在程序中使用绝对路径。
(1)在jar包同级目录创建配置文件conf.properties并写入配置数据:

confData=data


(2)开始写入自动化测试代码

//java项目www fhadmin org
public class Test{
    public String getData() throws IOException {
       //获取jar包同级目录
        String path = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
        String[] pathSplit = path.split("/");
        String jarName = pathSplit[pathSplit.length - 1];
        String jarPath = path.replace(jarName, "");
        String pathName=jarPath+"minhang.properties";
        System.out.println("配置文件路径:"+jarPath);
        //读取配置文件
        Properties properties = new Properties();
        File file = new File(pathName);
        FileInputStream fis = new FileInputStream(file);
        properties.load(fis);
        fis.close();
        //获取配置文件数据
        String confData = properties.getProperty("confData");
        System.out.println(confData);
    }
}


(3)执行jar包

java -jar jarNanexxx

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中,可以使用`ResourceLoader`来读取`resources`目录中的文件。下面是一个示例代码,演示如何使用`ResourceLoader`读取资源文件: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; import org.springframework.stereotype.Component; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; @Component public class ResourceReader { private final ResourceLoader resourceLoader; @Autowired public ResourceReader(ResourceLoader resourceLoader) { this.resourceLoader = resourceLoader; } public String readResourceFile(String filePath) throws IOException { Resource resource = resourceLoader.getResource("classpath:" + filePath); Path path = resource.getFile().toPath(); byte[] bytes = Files.readAllBytes(path); return new String(bytes, StandardCharsets.UTF_8); } } ``` 在上述代码中,我们创建了一个名为`ResourceReader`的组件,并通过构造函数注入了`ResourceLoader`。`readResourceFile`方法接受一个文件路径作为参数,可以是相对于`resources`目录的相对路径或者绝对路径。 在`readResourceFile`方法中,我们首先使用`resourceLoader.getResource("classpath:" + filePath)`获取资源文件的`Resource`对象。然后,我们通过调用`getFile().toPath()`将其转换为`Path`对象。最后,我们使用`Files.readAllBytes(path)`读取文件的所有字节,并将其转换为字符串返回。 需要注意的是,这里的示例代码假设资源文件存在于Spring Boot应用程序的类路径下的`resources`目录中。如果文件不存在或路径不正确,将会抛出`IOException`。因此,建议在实际使用进行适当的错误处理。 请确保将`ResourceReader`组件注册到Spring Boot应用程序的上下文中,以便可以使用`@Autowired`注入。然后,您可以在其他组件或服务中使用`ResourceReader`来读取资源文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值