Spring AOP 各种拦截方法执行的顺序

11 篇文章 1 订阅

AOPImpl.java

==================

package salesdepart.service.app;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.*;


import org.springframework.core.annotation.Order;


import java.util.*;


@Aspect
@Order(2)
public class AOPImpl {

//apply to salesdepart.service.app'all classes's methods
@Before("execution(* salesdepart.service.app.*.*(..))")
public void applySecondauth(){
System.out.println("@Order(2) AOP@Before Apply applySecondauth() ===>");

System.out.println("@Order(2) AOP@Before Apply applySecondauth() <===");
}
 
@AfterReturning(returning="rvt"
, pointcut="execution(* salesdepart.service.app.*.*(..))")
public void log(Object rvt){
System.out.println("AOP@AfterReturning  begin ===>");
System.out.println("AOP apply AfterReturningAOP(). return value:" + rvt);
System.out.println("record it to logs file...");
System.out.println("AOP@AfterReturning end <==");
}

@After("execution(* salesdepart.service.app.*.*(..))")
public void release(JoinPoint jp)
{
System.out.println("AOP@After :begin ===>");
//返回被织入增强处理的目标方法
System.out.println("AOP@After: weave name: "
+ jp.getSignature().getName());
//访问执行目标方法的参数
System.out.println("AOP@After:this method args:"
+ Arrays.toString(jp.getArgs()));
//访问被增强处理的目标对象
System.out.println("AOP@After:target to be weavered: "
+ jp.getTarget());
System.out.println("AOP@After:ends <====");
}

 @Around("execution(* salesdepart.service.app.*.*(..))")
public Object processTx(ProceedingJoinPoint jp)
throws java.lang.Throwable
{
System.out.println("AOP@Around begin ===>");
//访问执行目标方法的参数
Object[] args = jp.getArgs();
//当执行目标方法的参数存在,
//且第一个参数是字符串参数
if (args != null && args.length > 0
&& args[0].getClass() == String.class)
{
//改变第一个目标方法的第一个参数
args[0] = "around changed your parameter";
}
//执行目标方法,并保存目标方法执行后的返回值
Object rvt = jp.proceed(args);
System.out.println("AOP@Around end <===");
return rvt;
}
 
 @AfterThrowing(throwing="ex"
, pointcut="execution(* salesdepart.service.app.*.*(..))")
public void doRecoveryActions(Throwable ex)
{
       System.out.println("AOP@AfterThrowing begin ===>");
System.out.println("AOP@AfterThrowing : exception from target method:" + ex);
System.out.println("AOP@AfterThrowing : take some actions");
System.out.println("AOP@AfterThrowing  end <===");
}


}


AOPImpl2.java

==================

package salesdepart.service.app;


import org.aspectj.lang.annotation.*;
import org.aspectj.lang.*;
import org.springframework.core.annotation.Order;
import java.util.Arrays;


@Aspect
@Order(1)
public class AOPImpl2 {
//定义Before增强处理执行 as first step
@Before("execution(* salesdepart.service.app.*.*(..))")
public void FirstStepAuth(JoinPoint jp)
{
System.out.println("@Order(1) AOP@Before: apply 1st auth check begin ==>");
System.out.println("@Order(1) AOP@Before: apply 1st auth check end <==");
}
}


SalesDepartEmployee .java

====================

package salesdepart.service.app;


import org.springframework.beans.factory.annotation.Autowired;      
import org.springframework.stereotype.*;


import java.util.*;


@Component("SalesEmployee")
public class SalesDepartEmployee implements employee {

//@Autowired默认按类型装配,@Resource默认按名称装配
private String name;

//默认按类型装配
private DummyDAO mydao;

public SalesDepartEmployee() {
System.out.println(" SalesDepartEmployee() called");
name=" default";
mydao=null;
}

@Override
public void displayInfo() {
 System.out.println("this is Sales depart' employee "+name);
}

public void setName(String name) {
this.name=name;
}

public String getName() {
return name;
}


}


BeanTest .java

===========

package salesdepart.service.app;
import org.springframework.context.*;
import org.springframework.context.support.*;


import java.util.*;
public class BeanTest { 
public static void main(String[] args) {
ApplicationContext ctx = new
ClassPathXmlApplicationContext("employee.xml");
employee p=(employee)ctx.getBean("SalesEmployee");
p.displayInfo();
}
}


=========

输出结果

=============


DummyDAO() called

 SalesDepartEmployee() called


@Order(1) AOP@Before: apply 1st auth check begin ==>
@Order(1) AOP@Before: apply 1st auth check end <==


AOP@Around begin ===>


@Order(2) AOP@Before Apply applySecondauth() ===>

@Order(2) AOP@Before Apply applySecondauth() <===


this is Sales depart' employee  default


AOP@Around end <===

AOP@After :begin ===>
AOP@After: weave name: displayInfo
AOP@After:this method args:[]
AOP@After:target to be weavered: salesdepart.service.app.SalesDepartEmployee@1e98b1c
AOP@After:ends <====
AOP@AfterReturning  begin ===>
AOP apply AfterReturningAOP(). return value:null
record it to logs file...

AOP@AfterReturning end <==


从上面可以看出:

Spring首先执行依赖注入,调用无参数构造函数,完成对象初始化。

当执行调用方法时候,

首先执行第一个

Before(1) begin

Before(1) end

Invoke() begin

Before(2) begin

Before(2) end

被拦截的方法执行()

invoke() end

after() begin

after() end

AfterReturning() begin

AfterReturning() end



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值