ResponsibilityChainInstantiationAwareBeanPostProcessor.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package cn.nosum.support.processor;
  2. import cn.nosum.support.annotation.ResponsibilityChainMethod;
  3. import cn.nosum.support.chain.BaseContext;
  4. import cn.nosum.support.chain.ResponsibilityChainException;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.apache.commons.lang3.StringUtils;
  7. import org.springframework.beans.BeansException;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
  10. import org.springframework.core.annotation.AnnotationUtils;
  11. import org.springframework.util.ReflectionUtils;
  12. import java.lang.reflect.Method;
  13. import java.lang.reflect.Parameter;
  14. import java.lang.reflect.Type;
  15. /**
  16. * ResponsibilityChain InstantiationAwareBeanPostProcessor。
  17. *
  18. * @author Young
  19. */
  20. @Slf4j
  21. public class ResponsibilityChainInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor {
  22. @Autowired
  23. private ResponsibilityChainProcessor<BaseContext> processor;
  24. @Override
  25. public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
  26. Method[] declaredMethods = ReflectionUtils.getAllDeclaredMethods(bean.getClass());
  27. for (Method declaredMethod : declaredMethods) {
  28. ResponsibilityChainMethod chainMethod = AnnotationUtils.findAnnotation(declaredMethod, ResponsibilityChainMethod.class);
  29. if (chainMethod != null) {
  30. if (StringUtils.isBlank(chainMethod.groupName())) {
  31. throw new ResponsibilityChainException("责任链groupName不能为空");
  32. }
  33. Parameter[] parameters = declaredMethod.getParameters();
  34. if (parameters.length != 1) {
  35. throw new IllegalArgumentException("参数数量不能大于1" + chainMethod.desc());
  36. }
  37. if (chainMethod.isReturn()) {
  38. Type returnType = declaredMethod.getGenericReturnType();
  39. if (!"boolean".equals(returnType.getTypeName())) {
  40. throw new IllegalArgumentException(String.format("%s方法的isReturn=true但是实际的返回值类型%s",declaredMethod.getName(), returnType.getTypeName()));
  41. }
  42. }
  43. for (Parameter parameter : parameters) {
  44. if (!parameter.getType().getSuperclass().getName().equals(BaseContext.class.getName())
  45. && !parameter.getType().getName().equals(BaseContext.class.getName())) {
  46. throw new IllegalArgumentException("参数只能为BaseContext desc:" + chainMethod.desc() + "className:" + bean.getClass().getName());
  47. }
  48. }
  49. if (chainMethod.isReturn()) {
  50. processor.addChain(declaredMethod.getName(), BaseContext::isExecuteNextNode, context -> {
  51. ReflectionUtils.makeAccessible(declaredMethod);
  52. if (chainMethod.isPrintTime()) {
  53. context.getStopWatch().start(String.format("groupName:%s name:%s",chainMethod.groupName(),declaredMethod.getName()));
  54. }
  55. Object result = ReflectionUtils.invokeMethod(declaredMethod, bean, context);
  56. if (chainMethod.isPrintTime()) {
  57. context.getStopWatch().stop();
  58. }
  59. log.info("当前执行的带有返回值的 {} groupName = {} result= {} order= {} desc= {}", chainMethod.isReturn(), chainMethod.groupName(), result, chainMethod.order(), chainMethod.desc());
  60. if (result != null) {
  61. return (boolean) result;
  62. } else {
  63. return true;
  64. }
  65. }, chainMethod.order(), chainMethod.groupName());
  66. } else {
  67. processor.addChainFromConsumer(declaredMethod.getName(), BaseContext::isExecuteNextNode, context -> {
  68. ReflectionUtils.makeAccessible(declaredMethod);
  69. log.info("当前执行的没有返回值的 {} groupName = {} order= {} desc= {}", chainMethod.isReturn(), chainMethod.groupName(), chainMethod.order(), chainMethod.desc());
  70. if (chainMethod.isPrintTime()) {
  71. context.getStopWatch().start(String.format("groupName:%s name:%s",chainMethod.groupName(),declaredMethod.getName()));
  72. }
  73. ReflectionUtils.invokeMethod(declaredMethod, bean, context);
  74. if (chainMethod.isPrintTime()) {
  75. context.getStopWatch().stop();
  76. }
  77. }, chainMethod.order(), chainMethod.groupName());
  78. }
  79. }
  80. }
  81. return bean;
  82. }
  83. }