JAVA大白代码_AOP - 大白浅谈java - 博客园

1、spring aop有两种配置方式:基于xml配置、基于注解配置,下面是基于xml的配置用法。

applicationContext.xml中:

com.dao.transactionDemo类中:

package com.dao;

import org.aspectj.lang.ProceedingJoinPoint;

public class HelloWorldServiceImpl {

//执行顺序1:前置通知

public void beforeAdvice(){

System.out.println("前置通知");

}

//执行顺序2:环绕通知

public void aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable{

System.out.println("开始环绕通知");

joinPoint.proceed();//被执行的方法s()会在此处运行

System.out.println("结束环绕通知");

}

//执行顺序3:返回通知

public void returnAdvice(String result) {

//被执行方法sayHelloWorld()若有String返回值,则在此处可以得到并输出,故此处

//将输出"Hello World";若无返回值,则什么也不输出

System.out.println("返回通知的结果为:" + result);

}

//执行顺序4:后置通知

public void afterAdvice(){

System.out.println("后置通知");

}

}

com.service.helloWorldServiceImpl类中:

public class HelloWorldServiceImpl{ public String sayHelloWorld() { System.out.println("sayHelloWorld的方法主体");

String s="Hello World"; return s; } }

main函数执行主体:

ApplicationContext applicationContext

= new ClassPathXmlApplicationContext("applicationContext.xml");

HelloWorldServiceImpl helloWorldServiceImpl= applicationContext.getBean("helloWorldServiceImpl");

helloWorldServiceImpl.sayHelloWorld();

main函数执行后输出结果:(正常)

开始环绕通知

前置通知

sayHelloWorld的方法主体

结束环绕通知

后置通知

返回通知的结果为:Hello World

(如果main中sayHelloWorld的方法主体抛出异常,则执行顺序:

开始环绕通知

前置通知

sayHelloWorld的方法主体

后置通知

返回通知的结果为:Hello World

)

上面的第二、三行均在环绕通知aroundAdvice()中输出

2、基于注解的配置用法

切面类:

@Component

@Aspect

public class TransactionDemo {

/*表达式execution(* com.hand.core.service.*.*.*(..)),第一个*表示任何返回值,com.hand.core.service.*.*.*代表com.hand.core.service包下的任何子包下的任何类的任何方法,(..)代表方法中的任何参数

*/

@Pointcut(value="execution(* com.hand.core.service.*.*.*(..))")

public void point(){

}

@Before(value="point()")

public void before(){

System.out.println("transaction before");

}

@After(value="point()")

public void after(){

System.out.println("transaction After");

}

@AfterReturning(value = "point()") //返回通知

public void afterReturning(){

System.out.println("transaction AfterReturning");

}

@Around("point()")

public void around(ProceedingJoinPoint joinPoint) throws Throwable{

System.out.println("transaction begin");

joinPoint.proceed();

System.out.println("transaction commit");

}

}

被切面类:

package com.hand.core.service.impl;、

@Service

public class UserServiceImpl implements UserService {

@Autowired

private UserDao userDao;

@Override

public void addUser(User user) {

System.out.println("添加成功");

}

}

配置bean:

测试代码:(main函数执行主体)

User user = new User();

userService.addUser(user);

main函数执行后输出结果:

transaction begin

transaction before

添加成功

transaction commit

transaction After

transaction AfterReturning

3、执行顺序图解

208399a861cb79969d236f829df32c94.png

8b8548e8e7da70d66b76d0457dfb0238.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值