Skip to content

Commit

Permalink
优化反射中获取指定翻译通道,避免如果翻译通道的包中有多个类,会只寻找实现了翻译接口的类
Browse files Browse the repository at this point in the history
  • Loading branch information
xnx3 committed Jan 13, 2024
1 parent b2740b1 commit 65a90a4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ public class Service{
/**
* 获取当前使用的翻译服务
* @return
* @deprecated
*/
public static ServiceInterface getService() {
if(serviceInterface == null) {
System.err.println("cn.zvo.translate.tcdn.core.service.serviceInterface 尚未初始化,请检查是否加入了 tcdn.service 服务");
System.err.println("未配置短信通道,请在application.propeties 中进行配置。可参考 http://translate.zvo.cn/41160.html");
}
return serviceInterface;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.stereotype.Component;
import com.xnx3.ScanClassUtil;

import cn.zvo.translate.tcdn.core.service.Service;
import cn.zvo.translate.tcdn.core.service.ServiceInterface;
import cn.zvo.translate.tcdn.core.vo.TranslateResultVO;
import net.sf.json.JSONArray;
Expand Down Expand Up @@ -58,7 +59,8 @@ public static ServiceInterface getServiceInterface(HttpServletRequest request, H
return (ServiceInterface) o;
}
}
return null;

return Service.getService();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import com.sun.tools.javac.code.Type.ForAll;
import com.xnx3.Log;
import com.xnx3.ScanClassUtil;
import cn.zvo.translate.tcdn.core.service.Language;
Expand Down Expand Up @@ -43,7 +45,7 @@ public void run(String... args) throws Exception {
e.printStackTrace();
}
}
Log.info("language : \n"+JSONObject.fromObject(Language.map));
//Log.info("language : \n"+JSONObject.fromObject(Language.map));

//从application.properties中获取设置的默认翻译源
com.xnx3.Log.debug("load translate config by application.properties / yml : "+this.translateServiceApplicationConfig);
Expand Down Expand Up @@ -81,11 +83,21 @@ public void loadConfig(ApplicationConfig config) {

try {
Class serviceClass = classList.get(i);
Object newInstance = serviceClass.getDeclaredConstructor(Map.class).newInstance(entry.getValue());
Service.serviceInterface = (ServiceInterface) newInstance;
Service.serviceInterface.setLanguage(); //初始化设置语种
com.xnx3.Log.info("service use "+serviceClass.getName());
return;
Class[] interfaceClass = serviceClass.getInterfaces();
if(interfaceClass.length < 1) {
continue;
}
for(int j = 0; j<interfaceClass.length; j++) {
if("cn.zvo.translate.tcdn.core.service.ServiceInterface".equalsIgnoreCase(interfaceClass[0].getName())) {

Object newInstance = serviceClass.getDeclaredConstructor(Map.class).newInstance(entry.getValue());
Service.serviceInterface = (ServiceInterface) newInstance;
Service.serviceInterface.setLanguage(); //初始化设置语种
com.xnx3.Log.info("translate service use : "+serviceClass.getName());
return;
}
}

} catch (InstantiationException | IllegalAccessException | IllegalArgumentException| InvocationTargetException | NoSuchMethodException | SecurityException e) {
e.printStackTrace();
}
Expand Down

0 comments on commit 65a90a4

Please sign in to comment.