Spring学习总结:七:切点表达式、 通知的类型和用法、 基于注解的AOP配置

1. 切点表达式

2. 通知的类型和用法(基于Xml)

3. 基于注解的AOP配置

一.切点表达式

涉及到的通配符:

 

<!-- AOP的配置 -->
<aop:config>
<!-- public void insertUser(){
System.out.println("UserService创建用户");
} -->
<!-- 配置切入点 ,需要配合切点表达式说明 -->
<!-- "execution(public * com.softeem..*.*(..))"代表切面作用在com.softeem包及其子
包下的所有类的所有方法上 -->
<!-- void com.softeem.service.UserService.queryUser(String) -
->
<!-- <aop:pointcut id="firstPC" expression="execution(public * com.softeem..*.*
(..))"/>-->
<!-- <aop:pointcut id="firstPC" expression="execution(public *
com.softeem..*.query*(..))"/>-->
<!-- <aop:pointcut id="firstPC" expression="execution(*
com.softeem..*.queryUser(..))"/>-->
<!-- <aop:pointcut id="firstPC" expression="execution(Integer com.softeem..*.*
(..))"/>-->
<aop:pointcut id="firstPC" expression="execution(* com.softeem..*.*
(*,String))"/>
<!-- 定义切面 -->
<aop:aspect ref="myAspect">
<aop:before method="logExecuteTime" pointcut-ref="firstPC" />
</aop:aspect>
</aop:config
public class UserService {
public void insertUser(){
System.out.println("UserService创建用户");
}
public void queryUser(String id){
System.out.println("UserService查询到id为"+id+"的用户");
}
public void queryUserWithTwoParam(String id,String username){
System.out.println("UserService查询到id为"+id+",用户名为"+username+"的用户");
}
public String queryUserWithReturn(String id){
System.out.println("UserService查询到id为"+id+"的用户");
return "我是String返回值";
}
}
public class App {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new
ClassPathXmlApplicationContext("classpath:applicationContext.xml");
//
UserService userService = context.getBean("userService", UserService.class);
userService.insertUser();
System.out.println("===========");
userService.queryUser("100");
System.out.println("===========");
userService.queryUserWithTwoParam("200","木子");
System.out.println("===========");
userService.queryUserWithReturn("300");
System.out.println("===========");
}
}

二、.SpringAOP通知

1. SpringAOP通知分为5种类型

(1)、After returning advice:

<aop:after-returning method="afterReturning" returning="result"
pointcut-ref="firstPC" />
public void afterReturning(JoinPoint joinPoint,Object result) {
System.out.println("afterReturning通知");
System.out.println("方法返回值为:"+result);
}

(2)、After throwing advice

<aop:after-throwing method="afterThrowing" throwing="e"
pointcut-ref="firstPC" />
// public void afterThrowing(JoinPoint joinPoint,Exception e) {
public void afterThrowing(JoinPoint joinPoint,Throwable e) {
System.out.println("afterThrowing通知");
System.out.println(e.getMessage());
}

(3)、After advice

<aop:after method="after" pointcut-ref="firstPC" />
public void after(JoinPoint joinPoint){
System.out.println("after通知");
}

(4)、Around Advice

public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws
Throwable {
try {
System.out.println(joinPoint.getSignature()+"被执行了");
long start = System.currentTimeMillis();
//执行方法
Object result = joinPoint.proceed();
long end = System.currentTimeMillis();
if (end-start>=3000) {
System.out.println(joinPoint.getSignature().getName() + "方
法执行了" + (end - start) + "毫秒");
}
System.out.println("在环绕通知里获取目标方法的返回值:"+result);
return result;
} catch (Throwable e) {
//写入日志文件,我们这里就简单打印
System.out.println("发生异常了,异常信息为:" + e.getMessage());
//抛出异常,避免事务不回滚
throw e;
}
}
<aop:around method="aroundAdvice" pointcut-ref="firstPC" />

三、基于注解的AOP配置

<context:component-scan base-package="com.softeem"/>
<!-- 开启SpringAOP注解开发模式-->
<aop:aspectj-autoproxy/>
@Component
@Aspect
public class MyAspect2 {
@Pointcut("execution(public * com.softeem..*.*(..))")
public void myPointCut(){}
// @Before("execution(public * com.softeem..*.*(..))")
@Before("MyAspect2.myPointCut()")
public void beforeAdvice(JoinPoint joinPoint){
System.out.println("beforeAdvice通知");
}
@AfterReturning(value = "MyAspect2.myPointCut()",returning = "result")
public void afterReturning(JoinPoint joinPoint, Object result) {
System.out.println("afterReturning通知");
System.out.println("方法返回值为:" + result);
}
@AfterThrowing(value = "MyAspect2.myPointCut()",throwing = "e")
// public void afterThrowing(JoinPoint joinPoint,Exception e) {
public void afterThrowing(JoinPoint joinPoint, Throwable e) {
System.out.println("afterThrowing通知");
System.out.println(e.getMessage());
}
@After("MyAspect2.myPointCut()")
public void after(JoinPoint joinPoint) {
System.out.println("after通知");
}
// @Around("execution(public * com.softeem..*.*(..))")
@Around("MyAspect2.myPointCut2()")
public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
try {
System.out.println(joinPoint.getSignature()+"被执行了");
long start = System.currentTimeMillis();
//执行方法
Object result = joinPoint.proceed();
long end = System.currentTimeMillis();
System.out.println(joinPoint.getSignature().getName() + "方法执行了" + (end
- start) + "毫秒");
System.out.println("在环绕通知里获取目标方法的返回值:"+result);
return result;
// return "环绕通知随便返回的";
} catch (Throwable e) {
//写入日志文件,我们这里就简单打印
System.out.println("发生异常了,异常信息为:" + e.getMessage());
//抛出异常,避免事务不回滚
throwe;
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值