AOP:面向切面编程
spring的两大核心内容 1、ioc(控制反转):降低代码之间的耦合度。2、aop(面向切面编程):极大地提高了软件的可读性、复用性和可扩展性。
1、关注点
1.1核心关注点
系统中的核心功能业务逻辑,比如电子商务系统中的订单处理、客户管理、库存等
1.2横切关注点
分布在不同模块中解决相同的问题,比如电子商务系统中的用户验证,事务处理,日志管理等
2、连接点
用来定义在程序的哪里通过AOP加入新的逻辑,可以是方法、属性、构造函数、静态初始化块,甚至一条语句。比如打算登录时添加登录日志,那么下面方法即为一个连接点。
3、切入点
是一个或者多个连接点,看作是连接点的集合
4、导入依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.5</version>
</dependency>
Spring中配置使用
** 1、spring.xml进行配置(了解) **
<!--配置切面-->
<aop:config>
<!--切入点表达式-->
<aop:pointcut id="pointCut" expression="execution(* com.wanmait.mavendemo.service.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut"/>
</aop:config>
<aop:config>
<!--叫做logAspect的切面类-->
<aop:aspect ref="logAspect">
<!--切入点表达式-->
<aop:pointcut id="pointcut" expression="execution(* com.wanmait.mavendemo.service.impl.*.*(..)) and !execution(* com.wanmait.mavendemo.service.impl.LogServiceImpl.insert(*))"/>
<!--切面类before的方法为前置通知-->
<aop:before method="before" pointcut-ref="pointcut"/>
<!--切面类after的方法为后置通知-->
<aop:after method="after" pointcut-ref="pointcut"/>
<!--切面类after的方法为返回参数的后置通知-->
<aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="returnVal"/>
<!--切面类around的方法为环绕通知-->
<aop:around method="around" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
附上logAspect代码
@Component
public class LogAspect {
@Resource
private LogService logService;
//前置通知
public void before(JoinPoint point)
{
System.out.println("before...............");
System.out.println("目标类"+point.getTarget().getClass().getName());
System.out.println("方法"+point.getSignature().getName());
System.out.println("参数"+Arrays.asList(point.getArgs()));
}
//后置通知
public void after(JoinPoint point)
{
System.out.println("after.................");
}
//带返回值的后置通知
public void afterReturning(JoinPoint point,Object returnVal)
{
System.out.println("after returning......");
System.out.println(returnVal);
if(returnVal!=null)
{
System.out.println(returnVal.getClass().getName());
}
}
//环绕通知
public Object around(ProceedingJoinPoint point) throws Throwable {
System.out.println("around before..........");
long begin = System.currentTimeMillis();
//调用目标方法
Object result = point.proceed();
long end = System.currentTimeMillis();
System.out.println("around after..........");
System.out.println(point.getSignature().getName()+"执行时间"+(end-begin)+"毫秒");
return result;
}
}
2、注解的方法(用注解即可,springboot项目方便)
spring.xml中
<!--aop注解驱动-->
<aop:aspectj-autoproxy/>
<!--加上这个时候便可以使用注解了-->
在类上加入@Aspect注解便可以成为一个切片类
附上logAnnoAspect代码
@Component
@Aspect
public class LogAnnoAspect {
@Resource
private LogService logService;
//声明切入点表达式
@Pointcut("execution(* com.wanmait.mavendemo.service.impl.*.*(..)) " +
"&& !execution(* com.wanmait.mavendemo.service.impl.LogServiceImpl.insert(*))" +
"&& !execution(* com.wanmait.mavendemo.service.impl.AdminServiceImpl.findByUsername(..))")
public void pointCut(){
}
/* <!--前置通知 括号内代表使用哪个切入点,都什么方法时候用-->
@Before("pointCut()")
public void before(JoinPoint point)
{
System.out.println("before............");
}
@After("pointCut()")
public void after(JoinPoint point)
{
System.out.println("after..............");
}
*/
@AfterReturning(value = "pointCut()",returning = "returnVal")
public void afterReturning(JoinPoint point,Object returnVal)
{
System.out.println("after returning..........");
Log log = new Log();
Admin admin = null;
String ip = null;
try {
ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.currentRequestAttributes();
HttpSession session = requestAttributes.getRequest().getSession();
admin = (Admin)session.getAttribute("admin");
//ip = requestAttributes.getRequest().getRemoteAddr();
ip = IPUtil.getIpAddress(requestAttributes.getRequest());
} catch (IllegalStateException e) {
}
log.setAdmin(admin);
log.setIp(ip);
StringBuilder info = new StringBuilder();
info.append("目标类:").append(point.getTarget().getClass().getName());
info.append(",目标方法:").append(point.getSignature().getName());
info.append(",参数").append(Arrays.asList(point.getArgs()));
if(returnVal!=null) {
info.append(",返回类型").append(returnVal.getClass().getName());
info.append(",返回值").append(returnVal);
}
log.setInfo(info.toString());
logService.insert(log);
}
/*@Around("pointCut()")
public Object around(ProceedingJoinPoint point) throws Throwable {
System.out.println("around before............");
Object result = point.proceed();
System.out.println("around after..............");
return result;
}*/
}
RequestContextHolder.currentRequestAttributes()中可以获取requestAttributes
ServletRequestAttributes requestAttributes =(ServletRequestAttributes)RequestContextHolder.currentRequestAttributes();
HttpSession session = requestAttributes.getRequest().getSession();