springboot2.0 读取配置文件是通过org.springframework.boot.context.properties.bind.JavaBeanBinder 进行参数的绑定。
参数的处理是在org.springframework.boot.context.properties.source.ConfigurationPropertyName进行处理。此类springboot2.0新加的类。
下列方法对yml文件中的key进行处理的过程
static ConfigurationPropertyName adapt(CharSequence name, char separator,
Function<CharSequence, CharSequence> elementValueProcessor) {
Assert.notNull(name, "Name must not be null");
Assert.notNull(elementValueProcessor, "ElementValueProcessor must not be null");
if (name.length() == 0) {
return EMPTY;
}
List<CharSequence> elements = new ArrayList<>();
process(name, separator, (elementValue, start, end, indexed) -> {
elementValue = elementValueProcessor.apply(elementValue);
if (!isIndexed(elementValue)) {
elementValue = cleanupCharSequence(elementValue,
(ch, index) -> ch != '_' && !ElementValidator
.isValidChar(Character.toLowerCase(ch), index),
CharProcessor.NONE);
}
if (elementValue.length() > 0) {
elements.add(elementValue);
}
});
return new ConfigurationPropertyName(elements.toArray(new CharSequence[0]));
}
private static void process(CharSequence name, char separator,
ElementProcessor processor) {
int start = 0;
boolean indexed = false;
int length = name.length();
int openBracketCount = 0;
for (int i = 0; i < length; i++) {
char ch = name.charAt(i);
if (ch == ']') {
openBracketCount--;
if (openBracketCount == 0) {
processElement(processor, name, start, i + 1, indexed);
start = i + 1;
indexed = false;
}
}
else if (ch == '[') {
openBracketCount++;
if (!indexed) {
processElement(processor, name, start, i, indexed);
start = i;
indexed = true;
}
}
else if (!indexed && ch == separator) {
processElement(processor, name, start, i, indexed);
start = i + 1;
}
}
processElement(processor, name, start, length, false);
}
private static void processElement(ElementProcessor processor, CharSequence name,
int start, int end, boolean indexed) {
if ((end - start) >= 1) {
processor.process(name.subSequence(start, end), start, end, indexed);
}
}
private static CharSequence cleanupCharSequence(CharSequence name, CharFilter filter,
CharProcessor processor) {
for (int i = 0; i < name.length(); i++) {
char ch = name.charAt(i);
char processed = processor.process(ch, i);
if (filter.isExcluded(processed, i) || processed != ch) {
// We save memory by only creating the new result if necessary
StringBuilder result = new StringBuilder(name.length());
result.append(name.subSequence(0, i));
for (int j = i; j < name.length(); j++) {
processed = processor.process(name.charAt(j), j);
if (!filter.isExcluded(processed, j)) {
result.append(processed);
}
}
return result;
}
}
return name;
}
processElement方法会调用adapt方法的
process(name, separator, (elementValue, start, end, indexed) -> {
elementValue = elementValueProcessor.apply(elementValue);
if (!isIndexed(elementValue)) {
elementValue = cleanupCharSequence(elementValue,
(ch, index) -> ch != '_' && !ElementValidator
.isValidChar(Character.toLowerCase(ch), index),
CharProcessor.NONE);
}
if (elementValue.length() > 0) {
elements.add(elementValue);
}
});这里对key进行处理如果字符不是数字 字母将会被剔除掉
elementValue = cleanupCharSequence(elementValue,(ch, index) -> ch != '_' && !ElementValidator
.isValidChar(Character.toLowerCase(ch), index),CharProcessor.NONE);
shiro在进行filterChainDefinitions配置时,如果使用map读取yml配置文件时,
spring:
shiro:
filterChainDefinitions:
/static/**: anon
会变成 static:anon
出现此类问题可以通过
spring:
shiro:
filterChainDefinitions: |
/static/**: anon
/login: anon
字符串换行的形式进行数据接受。
factoryBean.setFilterChainDefinitions(filterChainDefinitions);