Spring AOP:自动注入参数值
import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; import java.lang.reflect.Method; @Aspect @Component public class HandleParamAop { @Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)") public void handleParamAop() { } @Before("handleParamAop()") public void paramHandler(JoinPoint joinPoint) throws Throwable { Object[] argc = joinPoint.getArgs(); Class clazz; Method[] methods; for (Object object : argc) { if (null == object) { continue; } clazz = object.getClass(); methods = clazz.getMethods(); // 这里的methods会包含父类的public方法,也包括Object类的method for (Method method : methods) { if (method.getName().equals("setCreator")) { method.invoke(object, 49L); } if (method.getName().equals("setModifier")) { method.invoke(object, 59L); } } } } }