Springboot如何设置外部目录为classpath?

风云扬 风云扬 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
普通的jar包:
通过java读取classpath下的properties配置文件内容,如下“代码段一”;此时,命令行通过
java -classpath ./testClasspath.jar:./test.properties com.devin.TestClasspath指定外部的classpath,代码依然能读取test.properties的配置内容, test.properties可以放到任意位置,只要在-classpath指定该路径就可以

Springboot jar包运行模式:
假设此时springboot的工程依赖上面提到的testClasspath.jar包,需要把springboot打成jar包,通过java -jar springboot.jar的方式启动服务;但springboot不支持设置-classpath,所以无法为testClasspath.jar指定外部test.properties文件的路径

问题是:springboot是否有什么方式可以设置外部目录成为classpath,让其依赖的其它jar包能读取到classpath下的配置文件?
之所以有这个问题,是因为有些配置文件不是springboot项目维护的(比如YARN的core-site.xml),只能通过指定classpath的方式为hadoop的jar包读取文件的目录


代码段一:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

public class TestClasspath{
private static String keyValueSeparators = "=: \t\r\n\f";
private static String strictKeyValueSeparators = "=:";
private static String specialSaveChars = "=: \t\r\n\f#!";
private static String whiteSpaceChars = " \t\r\n\f";

public static void main(String[] args){
try
{
InputStream is = TestClasspath.class.getClassLoader().getResourceAsStream("test.properties");
Properties testProperties = new Properties();
load(testProperties, is, "utf-8");

Set<Entry<Object, Object>> propertiesSet = testProperties.entrySet();
for (Map.Entry<Object, Object> entry : propertiesSet)
{
String sparkConfKey = entry.getKey().toString();
String sparkConfValue = entry.getValue().toString();

System.out.println(sparkConfKey + " = " + sparkConfValue);
}

}
catch (Exception e)
{
e.printStackTrace();
}

}

public static void load(Properties properties, InputStream inStream, String encoding)
throws IOException
{
InputStreamReader reader = new InputStreamReader(inStream, encoding);

load(properties, reader);
}

public static void load(Properties properties, Reader reader) throws IOException
{
BufferedReader in = new BufferedReader(reader);

while (true)
{
// Get next line
String line = in.readLine();

if (line == null)
{
return;
}

if (line.length() > 0)
{
// Find start of key
int len = line.length();
int keyStart;

for (keyStart = 0; keyStart < len; keyStart++)
{
if (whiteSpaceChars.indexOf(line.charAt(keyStart)) == -1)
{
break;
}
}

// Blank lines are ignored
if (keyStart == len)
{
continue;
}

// Continue lines that end in slashes if they are not comments
char firstChar = line.charAt(keyStart);

if ((firstChar != '#') && (firstChar != '!'))
{
while (continueLine(line))
{
String nextLine = in.readLine();

if (nextLine == null)
{
nextLine = "";
}

String loppedLine = line.substring(0, len - 1);

// Advance beyond whitespace on new line
int startIndex;

for (startIndex = 0; startIndex < nextLine.length(); startIndex++)
{
if (whiteSpaceChars.indexOf(nextLine.charAt(startIndex)) == -1)
{
break;
}
}

nextLine = nextLine.substring(startIndex, nextLine.length());
line = new String(loppedLine + nextLine);
len = line.length();
}

// Find separation between key and value
int separatorIndex;

for (separatorIndex = keyStart; separatorIndex < len; separatorIndex++)
{
char currentChar = line.charAt(separatorIndex);

if (currentChar == '\\')
{
separatorIndex++;
}
else if (keyValueSeparators.indexOf(currentChar) != -1)
{
break;
}
}

// Skip over whitespace after key if any
// test
int valueIndex;

for (valueIndex = separatorIndex; valueIndex < len; valueIndex++)
{
if (whiteSpaceChars.indexOf(line.charAt(valueIndex)) == -1)
{
break;
}
}

// Skip over one non whitespace key value separators if any
if (valueIndex < len)
{
if (strictKeyValueSeparators.indexOf(line.charAt(valueIndex)) != -1)
{
valueIndex++;
}
}

// Skip over white space after other separators if any
while (valueIndex < len)
{
if (whiteSpaceChars.indexOf(line.charAt(valueIndex)) == -1)
{
break;
}

valueIndex++;
}

String key = line.substring(keyStart, separatorIndex);
String value = (separatorIndex < len) ? line.substring(valueIndex, len) : "";

// Convert then store key and value
key = loadConvert(key);
value = loadConvert(value);
properties.put(key, value);
}
}
}
}

private static boolean continueLine(String line)
{
int slashCount = 0;
int index = line.length() - 1;

while ((index >= 0) && (line.charAt(index--) == '\\'))
{
slashCount++;
}

return ((slashCount % 2) != 0);
}

private static String loadConvert(String theString)
{
char aChar;
int len = theString.length();
StringBuffer outBuffer = new StringBuffer(len);

for (int x = 0; x < len; )
{
aChar = theString.charAt(x++);

if (aChar == '\\')
{
aChar = theString.charAt(x++);

if (aChar == 'u')
{
// Read the xxxx
int value = 0;

for (int i = 0; i < 4; i++)
{
aChar = theString.charAt(x++);

switch (aChar)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
value = ((value << 4) + aChar) - '0';
break;

case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
value = ((value << 4) + 10 + aChar) - 'a';
break;

case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
value = ((value << 4) + 10 + aChar) - 'A';
break;

default:
throw new IllegalArgumentException("Malformed \\uxxxx encoding.");
}
}

outBuffer.append((char) value);
}
else
{
if (aChar == 't')
{
aChar = '\t';
}
else if (aChar == 'r')
{
aChar = '\r';
}
else if (aChar == 'n')
{
aChar = '\n';
}
else if (aChar == 'f')
{
aChar = '\f';
}

outBuffer.append(aChar);
}
}
else
{
outBuffer.append(aChar);
}
}

return outBuffer.toString();
}
}
...全文

相关推荐

详解spring boot 使用application.properties 进行外部配置

主要介绍了详解spring boot 使用application.properties 进行外部配置,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

weixin_38681082

SpringBoot打包与排错[项目代码]

本文详细介绍了在Eclipse中使用Maven打包SpringBoot项目为JAR和WAR文件的方法,并提供了常见问题的解决方案。对于JAR包,文章涵盖了修改配置文件、POM文件调整、打包命令执行以及如何在Linux环境下运行JAR文件等内容。对于WAR包,则重点讲解了如何排除Tomcat服务器、部署到服务器以及访问方式。此外,文章还提供了获取包外位置的方法和解决Eclipse中找不到JAR文件的排错技巧,为开发者提供了全面的打包和部署指导。

ik678901

Spring Boot整合MyBatis加外部Tomcat启动-demo.zip

Spring Boot整合MyBatis加外部Tomcat启动的demo 日常的基础配置,不然每次都要从0开始,保存分享下!

升级打怪 iris

SpringBoot静态资源目录访问

今天小编就为大家分享一篇关于SpringBoot静态资源目录访问,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

weixin_38569722

Springboot引用外部配置文件的方法步骤

主要介绍了Springboot引用外部配置文件的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

weixin_38630358
评论 1
收藏 已收藏
分享
返回顶部
风云扬
Java EE 67,535 成员 225,843 内容