1、添加maven依赖
<!-- 切面 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.8.9</version>
</dependency>
2、代码实现样例
(1)、配置一个切面类如下:
定义切面实体,且注入容器中@Aspect,@Component
定义切点,可以为一个入口,也可以配多个,也可以配成所有
定义前置和后置方法
import java.util.Arrays;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
/**
* @Description: 学生切面
*/
@Aspect
@Component
public class StudentAspect {
private Logger logger = LoggerFactory.getLogger(this.getClass());
private long start = System.currentTimeMillis();
/** 定义一个切入点. 解释下: ~ 第一个 * 代表任意修饰符及任意返回值. ~ 第二个 * 任意包名 ~ 第三个 * 代表任意方法. ~ 第四个 *
* 定义在web包或者子包 ~ 第五个 * 任意方法 ~ .. 匹配任意数量的参数.
*/
@Pointcut("execution(public * com.zw.controller.StudentController.student(..))")
public void student() {
}
/**
* 前置通知
* @Title: doBefore
*/
@Before("student()")
public void doBefore(JoinPoint joinPoint) {
// 接收到请求,记录请求内容
logger.info("Aspect.doBefore()");
start = System.currentTimeMillis();
logger.info(""+start);
ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder
.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
// 记录下请求内容
logger.info("URL : " + request.getRequestURL().toString());
logger.info("HTTP_METHOD : " + request.getMethod());
logger.info("IP : " + request.getRemoteAddr());
logger.info("CLASS_METHOD : "
+ joinPoint.getSignature().getDeclaringTypeName() + "."
+ joinPoint.getSignature().getName());
logger.info("ARGS : " + Arrays.toString(joinPoint.getArgs()));
// 获取所有参数方法一:
Enumeration<String> enu = request.getParameterNames();
while (enu.hasMoreElements()) {
String paraName = (String)enu.nextElement();
System.out
.println(paraName + ": " + request.getParameter(paraName));
}
System.out.println("学生类学生方法切面之前");
}
/**
* 后置信息
* @Title: doAfterReturning
*/
@AfterReturning(value="student()",returning = "object")
public void doAfterReturning(JoinPoint joinPoint, Object object) {
// 处理完请求,返回内容
logger.info("Aspect.doAfterReturning()");
logger.info("耗时(毫秒) : "
+ (System.currentTimeMillis() - start));
System.out.println("学生类学生方法切面之后");
}
}
(2)、测试类
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import com.zw.base.BaseController;
import com.zw.domain.data.BeanConfig;
import com.zw.domain.data.DataSourceConfig;
@RestController
@RequestMapping(value = "student", method = {RequestMethod.POST, RequestMethod.GET})
public class StudentController extends BaseController{
@Value(value = "${zw.name}")
private String name;
@Value(value = "${zw.age}")
private String age;
@Autowired
private DataSourceConfig dataSourceConfig;
@Autowired
private BeanConfig beanConfig;
@RequestMapping("/student")
@ResponseBody
public String student() {
System.out.println("dataSourceConfig:"+JSON.toJSONString(dataSourceConfig));
System.out.println(111);
System.out.println("beanConfig:"+JSON.toJSONString(beanConfig));
logger.info("进入student函数");
Date date = new Date();
String a = name + " " + age + " " + date;
return a;
}
}
(3)、测试结果
这里除了学生类指定接口切面,还有一个全部的接口的controller切面,由以下的测试可以发现,更精确的切面会被先执行,且出入的顺序类似于栈结构的出入顺序(先进后出)。
3、以上仅为个人学习理解,学海无涯苦作舟,见一个学一个吧!