最近写一个数据接口的项目时候,因为想要通过去散发实现一对多的东西,@FunctionalInterface 注解恰好可以解决这个问题。我对这个注解不是很熟悉,基本上没用过。于是我就看了一下以前项目别人使用这个注解的代码,并尝试实现一下。
在使用 @FunctionalInterface 之前,可以了解一下什么是函数式接口(Functional Interface)?
函数式接口(Functional Interface)是 Java 8对一类特殊类型的接口的称呼。 这类接口只定义了唯一的抽象方法的接口(除了隐含的Object对象的公共方法), 因此最开始也就做SAM类型的接口(Single Abstract Method)。
在一个Java项目中,可以有任意数量的使用@FunctionalInterface
注解的方法。@FunctionalInterface
注解是用于标记函数式接口的,一个接口只有一个抽象方法时,就可以使用@FunctionalInterface
注解来确保该接口是一个函数式接口,从而可以使用Lambda表达式来实现该接口。
虽然一个函数式接口只能有一个抽象方法,但是它可以有任意数量的默认方法、静态方法和继承自Object
类的方法,这些方法不会影响接口的函数式属性。因此,一个使用@FunctionalInterface
注解的接口中可以包含多个这些方法。
话不多说,我们来看下代码,首先我们需要先定义一个函数式接口:
@FunctionalInterface
public interface DataHandlerService {
/**
* 数据入库
* @param dataCode
* @return
*/
Map<String, Object> handle(MultipartFile file, String dataCode);
}
接着在我们的公用service类中实现这个抽象方法,用字典引入这个处理的抽象方法。
@Slf4j
@Service
public class BatchDataService {
@Resource
private Map<String, DataHandlerService> dataHandlerServiceMap;
public Map<String, Object> importData(MultipartFile file, String dataCode) {
DataHandlerService service = dataHandlerServiceMap.get(dataCode + "Service");
return service.handle(file, dataCode);
}
}
在各个数据接口的ServiceImpl中实现抽象方法,并在@Service注解中标注service的默认值@Service("demoDataService")
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.util.*;
@Slf4j
@Service("demoDataService")
public class DemoDataServiceImpl implements DataHandlerService {
@Override
public Map<String, Object> handle(MultipartFile file, String dataCode) {
Map<String, Object> map = new HashMap<>();
try {
String batchId = CommonUtil.getUUID();
// todo 读取excel的dto,存入数据库
*
*
*
*
} catch (InterruptedException | ExecutionException e) {
throw new Exception("跑数据出现异常");
}
} catch (Exception e) {
throw new RuntimeException("导入数据或跑数据出现了异常");
}
return map;
}
}
Controller层直接调用这个方法,通过dataCode传参去访问对应的service,执行其数据的处理。
如上面demo的Service就传dataCode="demoData"。
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
@Slf4j
@RestController
@Api(tags = "数据导入")
@RequestMapping("Data")
public class BatchDataController {
@Resource
private BatchDataService batchDataService;
@PostMapping("/importData")
public Map<String, Object> importData(@RequestPart("file") MultipartFile file, String dataCode) {
Map<String, Object> resultData = new HashMap();
try {
// 校验文件格式和大小的
CommonMethod.uploadVerify(file);
Map<String, Object> importMap = batchCheckDataService.importData(file, dataCode);
resultData.put("code","200");
resultData.put("msg","操作成功");
resultData.put("data",importMap);
} catch (Exception e) {
log.error("批量导入数据出现异常:{}", e.getMessage(), e);
}
return resultData;
}
}
这样就可以用一个接口跳转多个service去处理了。