spring 切面简单整理

Spring 面向切面

1、 应用的场景:日志、安全和事务管理,应用时与应用的业务逻辑相分离

2、 横切关注点与业务逻辑相分离正是面向切面编程(AOP)所要解决的问题

3、 切面的组成部分:
a、通知(Advice): 切面应该完成的工作,定义切面何时才能使用。 Before、After、After-returning、After-throwing、Around

b、连接点(Join point): 连接点是在应用执行过程中能被插入切面的一个点。

c、切点(Poincut):何处执行切面,什么类还是什么包还是什么方法时候被执行。

d、切面(Aspect):通知和切点的结合,定义了他是什么,在何时和何处完成其功能。

e、织入(Weaving):是吧切面应用到目标对象并且创建新的代理对象的过程,切面在指定的连接点被注入到目标对象中。
时间:编译期、类加载期(JVM)、运行期(在织入切面时)

4、切点
execution(*concert.Performance.perform(..))&&within(concert*) //同样的方法还有 and/or/not &&/||/!
execution(*concert.Performance.perform(..))&&bean('woodsbook')
execution(*concert.Performance.perform(..))!bean('woodsbook')

5、 @After //通知方法返回之后
@AfterReturning //通知方法在目标返回后调用
@AfterThrowing //通知方法在目标跑出异常时候调用
@Around //通知方法将目标方法封装起来
@Before //通知方法会在目标方法调用之前执行

6、重复使用的时候
@Pointcut("*concert.Performance.perform(..)")
public void performance(){
}


7、<aop:aspectj-autoproxy /> //aop切面 Aspect 自动代理
AspectJ自动代理都会为使用@AspectJ注解的bean 创建一个代理,这个代理会围绕着所有该切面切点所匹配的Bean

8、环绕通知
@Around("performance()")
public void watchPerformance(ProceedingJoinPoint jp){
try{
System.out.println("Silencing cell phone");
System.out.println("Taking seats");
jp.proceed(); //被包含的方法的执行
System.out.println("CLAP CLAP CLAP !!!");

}catch{
System.out.println("Demanding a reFund ");
}

}

由于jp.proceed()代表的是调用的方法,所以我们可以让他在切面中多次的执行或者在切面中不让他执行

9、execution(* soundsystem.CompactDisc.playTrack(int))&&args(trackNumber)
有参数的通知
args 是说切面的方法接受的参数的名字必须是这个


10、@AfterReturning(value="addUser()", returning="user")
public void addUserAfterReturning(User user)throws Throwable{
this.syslogService.log(SyslogModule.SYSTEM, SyslogType.USER_ADD, String.format("添加操作员 [%s] 成功!", new Object[] { user.getLoginName() }));
}

returning 和方法中的参数 User user 是为切面的目标方法返回的类型
并且将返回类型传到切面的参数中
11、连接点(JoinPoint)
我们可以通过在切面的方法添加一个JoinPoint,这个值会由spring自动转入,我们从joinPoint中就可以获取
@Transactional
@AfterReturning(value = "todoTaskList()")
public void todoTaskListAfter(JoinPoint joinPoint) {
String taskName = CreditStepEnum.valueOf(G.str(joinPoint.getArgs()[0]))
.getStepDesc();
syslogService.log(SyslogModule.CREDIT,
SyslogType.BUSINESS_OPERATIONS,
String.format(CreditQueryLogRemarkFormatModal.creditToDoTaskList.getFormatModal(),
getLoginUserName(),
taskName));
}
















原文地址:https://www.cnblogs.com/Geyoung/p/6927905.html