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

风云扬 2018-06-01 03:27:15
普通的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();
}
}
...全文
3951 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
风云扬 2018-06-01
  • 打赏
  • 举报
回复
基于整个帖子: https://stackoverflow.com/questions/46728122/add-an-external-xml-file-containing-passwords-to-class-path-in-spring-boot 问题已经解决了,结贴。 补充2点: 1、经过试验,不需要在读取文件的时候前面加/(“you need to call getResourceAsStream("/myxml.xml") with a slash in the path.”) 2、如果需要配置对个外部目录,通过逗号分隔即可:java -Dloader.path="/myhomet/bb,/myhome/cc" -jar testclass-1.0.0.jar

67,550

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧