Implementing AOP in Java Controllers: Guidelines and Code Example
This article explains how to use Aspect‑Oriented Programming (AOP) in Java backend controllers by defining a unified ResultBean response format, outlining controller coding conventions, and providing complete Java and XML examples for logging, exception handling, and AOP configuration.
The author revisits previous posts about Java code quality and introduces a practical AOP implementation for Spring MVC controllers, emphasizing the importance of a unified ResultBean / PageResultBean return type to enable cross‑cutting concerns.
Key controller conventions are listed: all methods must return a standard ResultBean , the bean should not be passed to services, JSON or Map parameters are discouraged, Request/Response objects should be avoided in method signatures, and logging is delegated to the AOP layer.
The ResultBean is defined with generics and Lombok annotations (images omitted). The AOP class ControllerAOP intercepts controller methods, measures execution time, logs it, and centralizes exception handling. Known exceptions ( CheckException , UnloginException ) receive specific messages and codes, while unknown exceptions are logged and can trigger email notifications.
/**
* 处理和包装异常
*/
public class ControllerAOP {
private static final Logger logger = LoggerFactory.getLogger(ControllerAOP.class);
public Object handlerControllerMethod(ProceedingJoinPoint pjp) {
long startTime = System.currentTimeMillis();
ResultBean
result;
try {
result = (ResultBean
) pjp.proceed();
logger.info(pjp.getSignature() + "use time:" + (System.currentTimeMillis() - startTime));
} catch (Throwable e) {
result = handlerException(pjp, e);
}
return result;
}
private ResultBean
handlerException(ProceedingJoinPoint pjp, Throwable e) {
ResultBean
result = new ResultBean();
// 已知异常
if (e instanceof CheckException) {
result.setMsg(e.getLocalizedMessage());
result.setCode(ResultBean.FAIL);
} else if (e instanceof UnloginException) {
result.setMsg("Unlogin");
result.setCode(ResultBean.NO_LOGIN);
} else {
logger.error(pjp.getSignature() + " error ", e);
//TODO 未知的异常,应该格外注意,可以发送邮件通知等
result.setMsg(e.toString());
result.setCode(ResultBean.FAIL);
}
return result;
}
}The corresponding Spring XML configuration registers the AOP proxy and defines the pointcut that matches any public method returning ResultBean , linking it to the handlerControllerMethod advice.
Finally, the author stresses that a consistent interface definition is the prerequisite for effective AOP; the technique itself is simple, but disciplined coding habits and clear conventions are the real value.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.