1. 程式人生 > >Spring AOP 各種攔截方法執行的順序

Spring AOP 各種攔截方法執行的順序

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)

[email protected] Apply applySecondauth() ===>");

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

@After("execution(* salesdepart.service.app.*.*(..))")
public void release(JoinPoint jp)
{
System.out.println("
[email protected]
:begin ===>");
//返回被織入增強處理的目標方法
System.out.println("[email protected]: weave name: "
+ jp.getSignature().getName());
//訪問執行目標方法的引數
System.out.println("[email protected]:this method args:"
+ Arrays.toString(jp.getArgs()));
//訪問被增強處理的目標物件
System.out.println("[email protected]:target to be weavered: "
+ jp.getTarget());
System.out.println("[email protected]:ends <====");
}

 @Around("execution(* salesdepart.service.app.*.*(..))")
public Object processTx(ProceedingJoinPoint jp)
throws java.lang.Throwable
{
System.out.println("[email protected] 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("[email protected] end <===");
return rvt;
}
 
 @AfterThrowing(throwing="ex"
, pointcut="execution(* salesdepart.service.app.*.*(..))")
public void doRecoveryActions(Throwable ex)
{
       System.out.println("[email protected] begin ===>");
System.out.println("[email protected] : exception from target method:" + ex);
System.out.println("[email protected] : take some actions");
System.out.println("[email protected]  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) [email protected]: apply 1st auth check begin ==>");
System.out.println("@Order(1) [email protected]: 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) [email protected]: apply 1st auth check begin ==>
@Order(1) [email protected]: apply 1st auth check end <==

[email protected] begin ===>

@Order(2) [email protected] Apply applySecondauth() ===>

@Order(2) [email protected] Apply applySecondauth() <===

this is Sales depart' employee  default

[email protected] end <===

[email protected] :begin ===>
[email protected]: weave name: displayInfo
[email protected]:this method args:[]
[email protected]:target to be weavered: [email protected]
[email protected]:ends <====
[email protected]  begin ===>
AOP apply AfterReturningAOP(). return value:null
record it to logs file...

[email protected] 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


相關推薦

Spring AOP 各種攔截方法執行順序

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

Spring AOP各種通知 以及執行順序

1.定義切面 </pre><pre name="code" class="java"><pre name="code" class="java">package test2; import org.aspectj.lang.JoinPo

Spring AOP獲取攔截方法的參數名稱跟參數值

mon ogg try cat obj 超過 con 同時 point Spring AOP獲取攔截方法的參數名稱跟參數值 註意:這種方式需要JDK1.8版本支持 開始: 1.aop配置: <aop:aspectj-autoproxy expose-proxy

Spring例項化時各種初始化方法執行順序

帶序號的步驟來自Spring實戰(第4版),紅字為補充部分。 1.Spring對bean進行例項化; Constructor構造方法 2.Spring將值和bean的引用注入到bean對應的屬性中; 3.如果bean實現了BeanNameAware介面,Spring將bean的ID傳遞給

Spring 生命週期 各種初始化方法 執行順序

0、BeanFactoryPostProcessor呼叫postProcessBeanFactory方法 1、BeanPostProcessor#postProcessBeforeInitialization (1.1 @PostConstruct(CommonAnn

Spring Bean中初始化方法執行順序

1 當@Scope為singleton時,bean會在ioc初始化時就被例項化,預設為singleton,可以配合@Lazy實現延時載入 2 當@Scope為prototype時,bean在ioc初始化時不會被例項化,只有在使用時,或者手動context.getBean()時才會被例項化 3 執行順

Spring AOP無法攔截Controller中的方法

想使用AOP Annotation配置Spring MVC的Controller進行攔截, 發現無法攔截Controller的方法, 卻可以攔截Service層的方法. 一開始: Spring的配置檔案applicat

spring aop無法攔截類內部的方法呼叫

1.概念 攔截器的實現原理就是動態代理,實現AOP機制。Spring 的代理實現有兩種:一是基於 JDK Dynamic Proxy 技術而實現的;二是基於 CGLIB 技術而實現的。如果目標物件實現了介面,在預設情況下Spring會採用JDK的動態代理實現AOP 2.問題 在類C中,方法A呼叫方法B, B方

Spring ——獲取IOC容器時,構造方法、set方法、類方法執行順序

contex config bean bubuko true src tex pro 清晰 1,首先,我們在ApplicationContext.xml中會寫下下面類的標示: <bean id="helloword" class="com.xt.fristsprin

JFinal 配置類 方法執行順序

處理器 模板引擎 jfinal 擴展 ant 數據 void route intercept 1、public void configConstant(Constants me) 常量配置方法,一般 在裏面 讀取 配置文件 2、public void configRoute

jQuery源碼05 (3653 , 3797) queue() : 隊列方法 : 執行順序的管理

jquer jquery exp cache 之前 ise 參數 last 匿名 //對外接口 jQuery.extend({ queue: function( elem, type, data ) {//入隊。元素、隊列名字、存進去的函數 //jQuer

Java構造塊,靜態代碼塊,構造方法執行順序

構造方法 就會 -1 static關鍵字 demo args http cnblogs pub 構造塊:直接寫在類中的代碼塊 靜態代碼垮:使用static關鍵字聲明的代碼塊 靜態代碼塊先於構造塊,構造方法執行,而且只在類加載時候執行一次,後面在類實例化的時候會先執行構造塊,

spring---aop(3)---Spring AOP攔截器鏈

ati handler 攔截器 odi hand zab chain 記載 封裝 寫在前面   時間斷斷續續,這次寫一點關於spring aop攔截器鏈的記載。至於如何獲取spring的攔截器,前一篇博客已經寫的很清楚(spring---aop(2)---Spring AO

SpringMVC自定義多個攔截執行順序

true pos out itl 表示 src blog lin 應用 一、正常流程下的攔截器(全部放行) 1.springMVC中攔截器實現這個接口HandlerInterceptor 第一個攔截器 HandlerInterceptor1 public cl

Java 子類父類構造方法執行順序

1 public class Test { 2 class Super { 3 int flag = 1; 4 5 Super() { 6 test(); 7 } 8 9 void te

Spring Aop 修改目標方法引數和返回值

首先使用spring-aop需要在spring的配置檔案中假如 一、新建註解 @Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented

java中靜態變數,靜態方法,靜態程式碼塊,構造程式碼塊,構造方法...執行順序

public class TestExeOrder { public TestExeOrder(){ System.out.println("類的構造方法執行了..."); } { System.out.println("構造程式碼塊執行了

單元測試JunitTest的方法執行順序

如果Run as —>Junit Test,執行含有@Test註釋的方法是,那麼所有註解方法都將被執行,所含的執行順序是: @BeforeClass ,@Before,@Test,@After,@AfterClass

spring專案中監控方法執行時間

當專案規模越來越大時,難免會遇到效能問題。尤其是多系統之間介面呼叫,所以新增時間監控很有必要。但是由於程式碼已經上線,所以要保證對程式碼的侵略性最小,所以Spring AOP可以解決這個問題。 首先

spring AOP獲取切面方法資訊 JoinPoint的用法

JoinPoint 物件 封裝了SpringAop中切面方法的資訊,在切面方法中新增JoinPoint引數,就可以獲取到封裝該方法資訊的物件 常用api ; 方法名 功能 Signature getSignature();