之前一直写.net,没玩过spring,一直没用过aop(面向切面编程)这类功能,当然不是说.net里面没有这类框架
开始上代码:
注解定义
package com.jiankunking.common;
import java.lang.annotation.*;
/**
* @author jiankunking
* @Date: 2016/8/15
* @Time: 11:09
* @annotation OperationLogger
*/
@Retention(RetentionPolicy.RUNTIME)//注解会在class中存在,运行时可通过反射获取
@Target(ElementType.METHOD)//目标是方法
@Documented//文档生成时,该注解将被包含在javadoc中,可去掉
public @interface OperationLogger
{
/**
* 模块名字
*/
String modelName() default "";
/**
* 操作类型
*/
String option();
}
AOP拦截部分
package com.jiankunking.common;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
/**
* @author jiankunking
* @Date: 2016/8/15
* @Time: 11:11
* @annotation SysLogAspect
*/
@Aspect
@Component
public class SysLogAspect
{
private static final Logger logger = Logger.getLogger(SysLogAspect.class);
@Pointcut("@annotation(com.jiankunking.common.OperationLogger)")
public void controllerAspect()
{
System.out.println("我是一个切入点");
}
/**
* 前置通知(Before advice) :在某连接点(JoinPoint)之前执行的通知,但这个通知不能阻止连接点前的执行。
* @param joinPoint
*/
@Before("controllerAspect()")
public void doBefore(JoinPoint joinPoint)
{
System.out.println("=====SysLogAspect前置通知开始=====");
//handleLog(joinPoint, null);
}
/**
* 后通知(After advice) :当某连接点退出的时候执行的通知(不论是正常返回还是异常退出)。
* @param joinPoint
*/
@AfterReturning(pointcut = "controllerAspect()")
public void doAfter(JoinPoint joinPoint)
{
System.out.println("=====SysLogAspect后置通知开始=====");
//handleLog(joinPoint, null);
}
/**
* 抛出异常后通知(After throwing advice) : 在方法抛出异常退出时执行的通知。
* @param joinPoint
* @param e
*/
@AfterThrowing(value = "controllerAspect()", throwing = "e")
public void doAfter(JoinPoint joinPoint, Exception e)
{
System.out.println("=====SysLogAspect异常通知开始=====");
//handleLog(joinPoint, e);
}
/**
* 环绕通知(Around advice) :包围一个连接点的通知,类似Web中Servlet规范中的Filter的doFilter方法。可以在方法的调用前后完成自定义的行为,也可以选择不执行。
* @param joinPoint
*/
//@Around("controllerAspect()")
//public void doAround(JoinPoint joinPoint)
//{
// System.out.println("=====SysLogAspect 环绕通知开始=====");
// //handleLog(joinPoint, null);
//}
/**
* 日志处理
*
* @param joinPoint
* @param e
*/
private void handleLog(JoinPoint joinPoint, Exception e)
{
try
{
//获得注解
OperationLogger logger = giveController(joinPoint);
if (logger == null)
{
return;
}
String signature = joinPoint.getSignature().toString(); // 获取目标方法签名
String methodName = signature.substring(signature.lastIndexOf(".") + 1,
signature.indexOf("("));
String longTemp = joinPoint.getStaticPart().toLongString();
String classType = joinPoint.getTarget().getClass().getName();
Class clazz = Class.forName(classType);
Method[] methods = clazz.getDeclaredMethods();
System.out.println("methodName: " + methodName);
for (Method method : methods)
{
if (method.isAnnotationPresent(OperationLogger.class)
&& method.getName().equals(methodName))
{
//OpLogger logger = method.getAnnotation(OpLogger.class);
String clazzName = clazz.getName();
System.out.println("clazzName: " + clazzName + ", methodName: "
+ methodName);
}
}
} catch (Exception exp)
{
logger.error("异常信息:{}", exp);
exp.printStackTrace();
}
}
/**
* 获得注解
* @param joinPoint
* @return
* @throws Exception
*/
private static OperationLogger giveController(JoinPoint joinPoint) throws Exception
{
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();
if (method != null)
{
return method.getAnnotation(OperationLogger.class);
}
return null;
}
}
在Maven中加入以下以依赖
4.0.0
com.mkyong.common
spring-mvc-log4j
war
1.0-SNAPSHOT
SpringMVC + Log4j
1.7
4.3.2.RELEASE
2.6.2
1.2
3.1.0
1.7.4
3.1
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-aop
${spring.version}
org.springframework
spring-aspects
${spring.version}
org.apache.logging.log4j
log4j-api
${log4j.version}
org.apache.logging.log4j
log4j-core
${log4j.version}
jstl
jstl
${jstl.version}
javax.servlet
javax.servlet-api
${servletapi.version}
provided
log4j
log4j
1.2.17
org.aspectj
aspectjrt
${org.aspectj-version}
javax.inject
javax.inject
1
cglib
cglib
${cglib.version}
org.apache.maven.plugins
maven-compiler-plugin
3.3
${jdk.version}
${jdk.version}
在spring-*.xml中加入spring支持,打开aop功能
/WEB-INF/pages/
注解也写好了,spring也配置好了,在controller里面怎么用呢?
Controller应用
package com.jiankunking.controller;
import com.jiankunking.common.OperationLogger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping(value = "/Welcome/", produces = "text/html;charset=UTF-8")
public class WelcomeController
{
@OperationLogger(modelName = "WelcomeController", option = "getWelcome")
@RequestMapping(value = "/getWelcome", method = RequestMethod.POST)
public void getWelcome()
{
//异常拦截测试
//int i = 9 / 0;
System.out.println("hello world");
}
}
如何测试呢?
从前端发起ajax请求controller,即可:
$.ajax({
type: "POST",
url: "/Welcome/getWelcome",
contentType: "application/json",
data: null,
success: function ()
{
// alert("22");
},
error: function ()
{
// alert("失败!");
}
});
效果如下: