1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| package com.nari.core.handler;
import cn.hutool.core.collection.ListUtil; import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.TimeInterval; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.context.annotation.CommonAnnotationBeanPostProcessor; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component;
import javax.annotation.Resource; import java.lang.reflect.Field; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Proxy; import java.util.List; import java.util.Map;
@Slf4j @Component @Order(Ordered.HIGHEST_PRECEDENCE) public class BasicControllerPostProcessor extends CommonAnnotationBeanPostProcessor {
private static final long serialVersionUID = -945664767382485314L;
@SuppressWarnings("NullableProblems") @Override public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) { String packageName = beanClass.getPackage().getName(); log.trace("beanName ---{},packageName:{}", beanName, packageName); List<String> per = ListUtil.of("com.nari.supervision.daily"); List<String> tail = ListUtil.of("outbound","personal"); if (per.stream().anyMatch(packageName::contains)) { if (tail.stream().map(v -> v + ".controller").anyMatch(packageName::contains)) { try { updateResourceName(beanClass, beanName, "basicService", "Controller", "Service"); } catch (Exception e) { e.printStackTrace(); return null; } } if (tail.stream().map(v -> v + ".service").anyMatch(packageName::contains)) { try { updateResourceName(beanClass, beanName, "basicMapper", "Service", "TransformImpl"); updateResourceName(beanClass, beanName, "basicDao", "Service", "Mapper"); } catch (Exception e) { e.printStackTrace(); return null; } } if (tail.stream().map(v -> v + "serviceImpl").anyMatch(packageName::contains)) { try { updateResourceName(beanClass, beanName, "basicMapper", "Service", "TransformImpl"); updateResourceName(beanClass, beanName, "basicDao", "Service", "Mapper"); } catch (Exception e) { e.printStackTrace(); return null; } } } return null; }
private void updateResourceName(Class<?> beanClass, String beanName, String destField, String searchString, String replaceString) throws NoSuchFieldException, IllegalAccessException { Field field = beanClass.getSuperclass().getDeclaredField(destField); Resource basicMapper = field.getAnnotation(Resource.class); InvocationHandler invocationHandler = Proxy.getInvocationHandler(basicMapper); Field memberValues = invocationHandler.getClass().getDeclaredField("memberValues"); memberValues.setAccessible(true); Map<String, Object> map = (Map<String, Object>) memberValues.get(invocationHandler); map.put("name", StringUtils.replace(beanName, searchString, replaceString)); } }
|