您的位置:首页 > 编程语言 > Java开发

十.SpringAOP的Advice(通知)类型及对编码实现的理解

2019-08-21 09:25 1691 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/qq_41857955/article/details/99935628

SpringAOP的四种类型

  • 前置通知类型:在目标方法前实施增强
  • 后置通知类型:在目标方法后实施增强
  • 环绕通知类型:在目标方法执行前后实施增强
  • 异常通知类型:在方法出现异常之后抛出
  • 引介通知类型:在目标方法中添加一些属性和方法

通过代码实现

通过一个简单的用户浏览商品的例子去理解前四种通知类型
1.定义一个接口ProductInfoService

package com.tb.service;

public interface ProductInfoService {
//用户名和商品名
public void browse(String name,String productname);
}

2.定义接口的实现类ProductInfoServiceImpl

package com.tb.service;

public class ProductInfoServiceImpl implements ProductInfoService {

@Override
public void browse(String name, String productname) {
//int a=5/0;测试异常通知
System.out.println("as");
//用一个循环方便观察测试
int i=20;
while(i>0){
System.out.println(i--);
}
}
}

3.定义测试四种异常的类AllLogAdvice

package com.tb.aop;

import java.text.SimpleDateFormat;
import java.util.Date;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

public class AllLogAdvice {
public void mybeforeAdvice(JoinPoint joinpoint) {
// 前置通知,获得参数
Object[] args =joinpoint.getArgs();
Date d = new Date();
String browser = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(d);
String logInfotext = args[0] + browser + "浏览" + args[1];
System.out.println(logInfotext);
}

public void myAfterAdvice(JoinPoint joinpoint) {
//后置通知,获取参数
Object[] args = joinpoint.getArgs();
Date d = new Date();
String browser = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(d);
String logInfotext = args[0] + browser + "浏览" + args[1];
System.out.println(logInfotext);
}

public void myexceptionAdvice(JoinPoint joinpoint) {
//异常通知
String classname = joinpoint.getClass().getName();
String methodname = joinpoint.getSignature().getName();
Date d = new Date();
String exceptdate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(d);
String logInfotext = exceptdate + classname + methodname + "异常";
System.out.println(logInfotext);
}

public void myArodAdvice(ProceedingJoinPoint pjoinPoint) throws Throwable {
//环绕通知
System.out.println("环绕通知开始执行!");
long starttime = System.currentTimeMillis();
pjoinPoint.proceed();
long endtime = System.currentTimeMillis();
String methodname = pjoinPoint.getSignature().getName();
String logInfotext = methodname + "time is" + (endtime - starttime);
System.out.println(logInfotext);
}

}

4.配置applicationContext.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
//添加AOP的命名空间
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
<bean id="productInfoService" class="com.tb.service.ProductInfoServiceImpl">
</bean>
<bean id="allLogAdvice" class="com.tb.aop.AllLogAdvice"></bean>
<!-- AOP切面编程:
<aop:aspect>只有一个通知和一个切入点
<aop:pointcut>切入点从具体的目标对象获取方法
-->
<aop:config>
<aop:aspect id="logaop" ref="allLogAdvice">
<aop:pointcut expression="execution(* com.tb.service.ProductInfoService.*(..))" id="logpoint"/>
<!--前置通知切入 -->
<aop:before method="mybeforeAdvice" pointcut-ref="logpoint"/>
<!--后置通知切入 -->
<aop:after-returning method="myAfterAdvice" pointcut-ref="logpoint"/>
<!--异常通知知切入 -->
<aop:after-throwing method="myexceptionAdvice" pointcut-ref="logpoint" throwing="e"/>
<!--环绕通知切入 -->
<aop:around method="myArodAdvice" pointcut-ref="logpoint"/>
</aop:aspect>
</aop:config>
</beans>

5.定义测试类TestAop

package com.tb.aop;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.tb.service.ProductInfoService;

public class TestAop {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
ProductInfoService ps=(ProductInfoService)ac.getBean("productInfoService");
ps.browse("張三", "華爲p10");
}
}

6.运行结果

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐