实际编程中,可能会有这样一种情况,前台传过来的参数,我们需要一定的处理才能使用
@Controller
public class MatchOddsController {
@Autowired
private MatchOddsServcie matchOddsService;
@RequestMapping(value = "/listOdds", method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE})
@ResponseBody
public List listOdds(@RequestParam Date startDate, @RequestParam Date endDate) {
return matchOddsService.listOdds(startDate, endDate);
}
}
前台传过来的startDate和endDate是两个日期,实际使用中我们需要将之转换为两个日期对应的当天11点,如果只有这么一个类的话,我们是可以直接在方法最前面处理就可以了
@Controller
public class MatchProductController {
@Autowired
private MatchProductService matchProductService;
@RequestMapping(value = "/listProduct", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
@ResponseBody
public List listProduct(@RequestParam Date startDate, @RequestParam Date endDate) {
return matchProductService.listMatchProduct(startDate, endDate);
}
}
@Controller
public class MatchController {
@Autowired
private MatchService matchService;
@RequestMapping(value = "/listMatch", method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE})
@ResponseBody
public List listMatch(@RequestParam Date startDate, @RequestParam Date endDate) {
return matchService.listMatch(startDate, endDate);
}
}
当然也可以写两个util方法,分别处理startDate和endDate,但是为了让Controller看起来更干净一些,我们还是用AOP来实现吧,顺便为AOP更复杂的应用做做铺垫
本应用中使用Configuration Class来进行配置,
@SpringBootApplication
@EnableAspectJAutoProxy(proxyTargetClass = true) //开启AspectJ代理,并将proxyTargetClass置为true,表示启用cglib对Class也进行代理
public class Application extends SpringBootServletInitializer {
...
}
@Aspect //1
@Configuration //2
public class SearchDateAspect {
@Pointcut("execution(* com.ronnie.controller.*.list*(java.util.Date,java.util.Date)) && args(startDate,endDate)") //3
private void searchDatePointcut(Date startDate, Date endDate) { //4
}
@Around(value = "searchDatePointcut(startDate,endDate)", argNames = "startDate,endDate") //5
public Object dealSearchDate(ProceedingJoinPoint joinpoint, Date startDate, Date endDate) throws Throwable { //6
Object[] args = joinpoint.getArgs(); //7
if (args[0] == null) {
args[0] = Calendars.getTodayEleven();
args[1] = DateUtils.add(new Date(), 7, TimeUnit.DAYS);//默认显示今天及以后的所有赔率
} else {
args[0] = DateUtils.addHours(startDate, 11);
args[1] = DateUtils.addHours(endDate, 11);
}
return joinpoint.proceed(args); //8
}
}
更多可参考
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html
http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/
由于项目中打印日志的需要,研究了一下在aop中,获取参数名称的方法。
Signature signature = joinpoint.getSignature(); MethodSignature methodSignature = (MethodSignature) signature; String[] strings = methodSignature.getParameterNames(); System.out.println(Arrays.toString(strings));

public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable{
String classType = joinPoint.getTarget().getClass().getName();
Class<?> clazz = Class.forName(classType);
String clazzName = clazz.getName();
String methodName = joinPoint.getSignature().getName(); //获取方法名称
Object[] args = joinPoint.getArgs();//参数
//获取参数名称和值
Map nameAndArgs = getFieldsName(this.getClass(), clazzName, methodName,args);
System.out.println(nameAndArgs.toString());
//为了省事,其他代码就不写了,
return result = joinPoint.proceed();
}
private MapgetFieldsName(Class cls, String clazzName, String methodName, Object[] args) throws NotFoundException { Map map=new HashMap (); ClassPool pool = ClassPool.getDefault(); //ClassClassPath classPath = new ClassClassPath(this.getClass()); ClassClassPath classPath = new ClassClassPath(cls); pool.insertClassPath(classPath); CtClass cc = pool.get(clazzName); CtMethod cm = cc.getDeclaredMethod(methodName); MethodInfo methodInfo = cm.getMethodInfo(); CodeAttribute codeAttribute = methodInfo.getCodeAttribute(); LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag); if (attr == null) { // exception } // String[] paramNames = new String[cm.getParameterTypes().length]; int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1; for (int i = 0; i < cm.getParameterTypes().length; i++){ map.put( attr.variableName(i + pos),args[i]);//paramNames即参数名 } //Map<> return map; }
以上为个人经验,希望能给大家一个参考,也希望大家多多支持趣讯吧。