Skip to content

Commit

Permalink
优化 JSONFinder
Browse files Browse the repository at this point in the history
  • Loading branch information
troyzhxu committed Apr 13, 2022
1 parent f1ec56d commit 62000c8
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions jsonkit-core/src/main/java/com/ejlchina/json/JSONFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,29 @@ public static void init(DataConvertor convertor) {
* 寻找 JSON 转换器
* @return DataConvertor
*/
public static synchronized DataConvertor find() {
public static DataConvertor find() {
// 如果存在,直接返回
if (_convertor != null) {
return _convertor;
}
// 使用 SPI 方式寻找 JSONFactory
for (JSONFactory factory : ServiceLoader.load(JSONFactory.class)) {
_convertor = factory.create();
if (_convertor != null) {
return _convertor;
synchronized (JSONFinder.class) {
if (_convertor == null) {
// 使用 SPI 方式寻找 JSONFactory
for (JSONFactory factory : ServiceLoader.load(JSONFactory.class)) {
_convertor = factory.create();
if (_convertor != null) {
break;
}
}
}
if (_convertor == null) {
// 从 System Properties 从寻找 JSONFactory
String className = System.getProperty(FACTORY_KEY);
if (className != null) {
JSONFactory factory = loadJSONFactory(className);
_convertor = factory.create();
}
}
}
// 从 System Properties 从寻找 JSONFactory
String className = System.getProperty(FACTORY_KEY);
if (className != null) {
JSONFactory factory = loadJSONFactory(className);
_convertor = factory.create();
}
if (_convertor != null) {
return _convertor;
Expand Down

0 comments on commit 62000c8

Please sign in to comment.