首先需要引入二个依赖
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
<version>2.5.5</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-jsr223</artifactId>
<version>2.5.5</version>
</dependency>
其次便是groovy的工具类,可写到utils中
public class GroovyUtil {
private static Map<String, GroovyObject> objectCache = new ConcurrentHashMap<>();
//groovy语句在文件中
public static Object invokeByFilePath(String scriptPath, String method, Object... objects)
throws IllegalAccessException, InstantiationException, IOException {
GroovyObject scriptInstance = null;
String cacheKey = SecureUtil.md5(scriptPath);
if(objectCache.containsKey(cacheKey)) {
scriptInstance = objectCache.get(cacheKey);
}
else {
//获取路径资源
ClassPathResource classPathResource = new ClassPathResource(scriptPath);
InputStream inputStream =classPathResource.getInputStream();
String scriptContent = IoUtil.read(inputStream, Charset.defaultCharset());
GroovyClassLoader classLoader = new GroovyClassLoader();
//将groovy语句内容转成一个class类
Class scriptClass = classLoader.parseClass(scriptContent);
//实例化groovyObject类
scriptInstance = (GroovyObject) scriptClass.newInstance();
}
//执行实例化的groovy类的的method这个方法,这个method方法也就是主方法。
return scriptInstance.invokeMethod(method, objects);
}
//groovy语句是个String时
public static Object invokeByContent(String scriptContent, String method, Object... objects)
throws IllegalAccessException, InstantiationException {
GroovyObject scriptInstance = null;
String cacheKey = SecureUtil.md5(scriptContent);
if(objectCache.containsKey(cacheKey)) {
scriptInstance = objectCache.get(cacheKey);
}
else {
GroovyClassLoader classLoader = new GroovyClassLoader();
Class scriptClass = classLoader.parseClass(scriptContent);
scriptInstance = (GroovyObject) scriptClass.newInstance();
}
return scriptInstance.invokeMethod(method, objects);
}
}
调用工具类GroovyUtil的方法执行groovy文件中的指定方法,且将参数传过去
makeLevelDTOList = (List<MakeLevelDTO>)GroovyUtil.invokeByFilePath(
"/script/MakeLevel.groovy", "makeList", normList, allLevels, targetLevelCode, outpatients);
关于groovy文件中的内容,例如我调用的在文件中的这个方法
public static List<MakeLevelDTO> makeList(List<ICFaInvestigationNorm> normList,
List<ICBmLevel> levelList,
Integer targetLevel,
Integer firstNormValue) {
init(levelList, targetLevel, firstNormValue);
Method[] methods = MakeLevel.class.declaredMethods;
List<MakeLevelDTO> list = new ArrayList<>();
for(ICFaInvestigationNorm norm : normList) {
String methodName = null;
String tempName = "rule" + norm.normCode.replaceAll("\\.", "_");
for (Method method : methods) {
if (method.name.equals(tempName)) {
methodName = tempName;
break;
}
}
if(norm.appendInfo == null) {
MakeLevelDTO makeLevelDTO = new MakeLevelDTO();
makeLevelDTO.normCode = norm.normCode;
makeLevelDTO.wholeScore = 0.00;
makeLevelDTO.fitTarget = 0;
makeLevelDTO.fitLevel = 0;
list.add(makeLevelDTO);
}
else {
methodName = methodName == null ? GENERAL_RULE.get(norm.appendInfo.scoreShow) : methodName;
if (methodName == null) {
continue;
}
Method method = MakeLevel.class.getDeclaredMethod(methodName, ICFaInvestigationNorm.class);
MakeLevelDTO makeLevelDTO = (MakeLevelDTO) method.invoke(null, norm);
list.add(makeLevelDTO);
}
}
return list;
}
在方法中也就是通过类反射获取文件中的所有的方法名,然后根据传过来的参数(也就是和写在groovy中的方法名相同的名称),method.invoke去遍历执行传过来的所有方法名参数,也就是执行符合的方法,计算出数据。