猿问

如何创建用于拆分请求参数并收集返回结果的自定义注释?

我有一个方法 params 是一个大于 50000 个项目的列表;限于业务逻辑,列表必须小于30000,这样我才有办法在逻辑之前把这个数组拆分成二维数组


public static final <T> Collection<List<T>> partitionBasedOnSize(List<T> inputList, int size) {

        AtomicInteger counter = new AtomicInteger(0);

        return inputList.stream().collect(Collectors.groupingBy(s -> counter.getAndIncrement() / size)).values();

}

这是我目前的解决方案:


public List<Account> getChildrenList(List<Long> ids) {

        List<Account> childrenList = new ArrayList<>();

        Collection<List<Long>> childrenId2dList = PartitionArray.partitionBasedOnSize(childrenIdsList, 30000);

        for (List<Long> list : childrenId2dList) {

            //this is my business logic: start

            childrenList.addAll(accountRepository.getAccounts(list));

            //this is my business logic: end

        }

        return childrenAccountsList;

}

我想在方法之上创建一个注释而不是许多重复的代码(每次都检查和恶意...)


对不起,我的英语不好,我画了一个图表:方法调用>恶意数组>业务逻辑>收集所有结果>返回

婷婷同学_
浏览 139回答 2
2回答

幕布斯7119047

在我看来,在这种情况下使用注释有点过度设计(您必须编写注释处理器)。您可以轻松地使用泛型和lambda 表达式和/或方法引用来实现您的目标。例如:以PartitionArray这种方式更新:import java.util.ArrayList;import java.util.Collection;import java.util.List;import java.util.concurrent.atomic.AtomicInteger;import java.util.function.Function;import java.util.stream.Collectors;public class PartitionArray {&nbsp; &nbsp; private static <T> Collection<List<T>> partitionBasedOnSize(List<T> inputList, int partitionSize) {&nbsp; &nbsp; &nbsp; &nbsp; Collection<List<T>> collection = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; int remainingSize = inputList.size();&nbsp; &nbsp; &nbsp; &nbsp; int index = 0;&nbsp; &nbsp; &nbsp; &nbsp; while (remainingSize > partitionSize) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; collection.add(inputList.subList(index, index + partitionSize));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; remainingSize -= partitionSize;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index += partitionSize;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; collection.add(inputList.subList(index, index + remainingSize));&nbsp; &nbsp; &nbsp; &nbsp; return collection;&nbsp; &nbsp; }&nbsp; &nbsp; public static <D, T> List<D> partitionAndDoBusinessFunction(List<T> ids, Function<List<T>, List<D>> businessFunction, int partitionSize) {&nbsp; &nbsp; &nbsp; &nbsp; List<D> dataList = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; Collection<List<T>> idListCollection = partitionBasedOnSize(ids, partitionSize);&nbsp; &nbsp; &nbsp; &nbsp; for (List<T> idList : idListCollection) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataList.addAll(businessFunction.apply(idList));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return dataList;&nbsp; &nbsp; }}然后只需从您的AccountService(使用方法参考)中使用它:import java.util.List;public class AccountService {&nbsp; &nbsp; private AccountRepository accountRepository;&nbsp; &nbsp; public List<Account> getAccounts(List<Long> ids) {&nbsp; &nbsp; &nbsp; &nbsp; return PartitionArray.partitionAndDoBusinessFunction(ids, accountRepository::getAccounts, 30000);&nbsp; &nbsp; }}或者使用 lambda 表达式:import java.util.List;public class AccountService {&nbsp; &nbsp; private AccountRepository accountRepository;&nbsp; &nbsp; public List<Account> getAccounts(List<Long> ids) {&nbsp; &nbsp; &nbsp; &nbsp; return PartitionArray.partitionAndDoBusinessFunction(ids, idList -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<Account> accounts = accountRepository.getAccounts(idList);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // do more business on accounts&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return accounts;&nbsp; &nbsp; &nbsp; &nbsp; }, 30000);&nbsp; &nbsp; }}

守着一只汪

这是使用annotations的解决方案,使用AspectJ:首先,定义所需的注释:package partition;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.METHOD)public @interface Partitioned {&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* The size of the partition, for instance 30000&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; int size();}因此,您的服务将如下所示:package partition;import java.util.List;public class AccountService {&nbsp; &nbsp; private AccountRepository accountRepository;&nbsp; &nbsp; public AccountService(AccountRepository accountRepository) {&nbsp; &nbsp; &nbsp; &nbsp; this.accountRepository = accountRepository;&nbsp; &nbsp; }&nbsp; &nbsp; @Partitioned(size = 30000)&nbsp; &nbsp; public List<Account> getAccounts(List<Long> ids) {&nbsp; &nbsp; &nbsp; &nbsp; return accountRepository.getAccounts(ids);&nbsp; &nbsp; }}到目前为止,这很容易。然后是注解的处理,AspectJ 进入游戏。定义链接到注释的方面:package partition;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Pointcut;import java.util.ArrayList;import java.util.Collection;import java.util.List;@Aspectpublic class PartitionedAspect {&nbsp; &nbsp; @Pointcut("@annotation(partitioned)")&nbsp; &nbsp; public void callAt(Partitioned partitioned) {&nbsp; &nbsp; }&nbsp; &nbsp; @Around("callAt(partitioned)")&nbsp; &nbsp; public <T, D> Object around(ProceedingJoinPoint pjp, Partitioned partitioned) throws Throwable {&nbsp; &nbsp; &nbsp; &nbsp; List<T> inputIds = (List) pjp.getArgs()[0];&nbsp; &nbsp; &nbsp; &nbsp; if (inputIds.size() > partitioned.size()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<D> dataList = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Collection<List<T>> partitionedIds = PartitionArray.partitionBasedOnSize(inputIds, partitioned.size());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (List<T> idList : partitionedIds) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<D> data = (List) pjp.proceed(new Object[]{idList});&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataList.addAll(data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return dataList;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return pjp.proceed();&nbsp; &nbsp; }}当然,您必须导入AspectJ,并在编译时做一些额外的事情。假设您使用的是 maven,请将这些行添加到您的pom.xml(插件和依赖项)中:<build>&nbsp; &nbsp; <plugins>&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; &nbsp; &nbsp; <plugin>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.codehaus.mojo</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>aspectj-maven-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>1.7</version>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <complianceLevel>1.8</complianceLevel>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <source>1.8</source>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <target>1.8</target>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <showWeaveInfo>true</showWeaveInfo>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <verbose>true</verbose>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Xlint>ignore</Xlint>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <encoding>UTF-8</encoding>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <executions>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <execution>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goal>compile</goal>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goal>test-compile</goal>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </execution>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </executions>&nbsp; &nbsp; &nbsp; &nbsp; </plugin>&nbsp; &nbsp; </plugins></build><dependencies>&nbsp; &nbsp; ...&nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.aspectj</groupId>&nbsp; &nbsp; &nbsp; &nbsp; <artifactId>aspectjrt</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; <version>${aspectj.version}</version>&nbsp; &nbsp; </dependency>&nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.aspectj</groupId>&nbsp; &nbsp; &nbsp; &nbsp; <artifactId>aspectjweaver</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; <version>${aspectj.version}</version>&nbsp; &nbsp; </dependency>...</dependencies>
随时随地看视频慕课网APP

相关分类

Java
我要回答