1. 程式人生 > >7.spring:SpringAOP(配置文件)

7.spring:SpringAOP(配置文件)

main ssp out cut 接受 bsp strong src app

SpringAOP(xml文件配置)

配置文件的方式,主要是在xml文件中進行配置,不使用註解!

目錄:

技術分享圖片

AtithmeticCalculator.java
public interface AtithmeticCalculator {
    int add(int i,int j);
    int sub(int i,int j);
    int mul(int i,int j);
    int div(int i,int j);
}
AtithmeticCalculatorImp1.java
@Component
public class AtithmeticCalculatorImp1 implements AtithmeticCalculator{
    
public int add(int i, int j) { int res = i + j; return res; } public int sub(int i, int j) { int res = i - j; return res; } public int mul(int i, int j) { int res = i * j; return res; } public int div(int i, int j) { int
res = i / j; return res; } }
LoggionAspect.java
public class LoggionAspect {
    public void beforeMethod(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        Object [] args = joinPoint.getArgs();
        System.out.println("The method " + methodName + "
begins with " + Arrays.asList(args)); } public void afterMethod(JoinPoint joinPoint){ String methodName = joinPoint.getSignature().getName(); System.out.println("The method " + methodName + " ends"); } public void afterReturning(JoinPoint joinPoint, Object result){ String methodName = joinPoint.getSignature().getName(); System.out.println("The method " + methodName + " ends with " + result); } public void afterThrowing(JoinPoint joinPoint, Exception e){ String methodName = joinPoint.getSignature().getName(); System.out.println("The method " + methodName + " occurs excetion:" + e); }
}
LoggionAspect2.java
public class LoggionAspect2 {
    public void beforeMethod1(JoinPoint joinPoint){
      System.out.println("--->beforeMethod1");
      String methodName = joinPoint.getSignature().getName();
      Object [] args = joinPoint.getArgs();
      System.out.println("The method " + methodName + " begins with " + Arrays.asList(args));
      System.out.println("--->beforeMethod1");
    }
    
    public void afterMethod1(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("The method " + methodName + " ends");
    }
    
    public void afterReturning1(JoinPoint joinPoint, Object result){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("The method " + methodName + " ends with " + result);
    }
    public void afterThrowing1(JoinPoint joinPoint, Exception e){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("The method " + methodName + " occurs excetion:" + e);
    }
    

applicationContext.xml

<!-- 配置bean -->
<bean id="atithmeticCalculator" class="com.MrChengsc.AOP.xml.AtithmeticCalculatorImp1"></bean>

<!-- 配置切面的Bean -->
<bean id="loggionAspect" class="com.MrChengsc.AOP.xml.LoggionAspect"></bean>
<bean id="loggionAspect2" class="com.MrChengsc.AOP.xml.LoggionAspect2"></bean>

<!-- 配置AOP -->
<aop:config>
    <!-- 配置切點 表達式-->
    <aop:pointcut expression="execution(* com.MrChengsc.AOP.xml.AtithmeticCalculator.*(int, int))" id="pointcut"/>
    
    <!-- 配置切面及通知 -->
    <aop:aspect  ref="loggionAspect2" order="2">
        <aop:before method="beforeMethod1" pointcut-ref="pointcut"/>
    </aop:aspect>
    <aop:aspect  ref="loggionAspect" order="1">
        <aop:before method="beforeMethod" pointcut-ref="pointcut"/>
    </aop:aspect>
</aop:config>

main

    public static void main(String[] args) {
//        AtithmeticCalculator atithmeticCalculator = null;
//        atithmeticCalculator = new AtithmeticCalculatorImp();
//        
//        atithmeticCalculator.add(1, 3);
//        System.out.println("---");
//        atithmeticCalculator.sub(3, 1);
//        System.out.println("---");
//        atithmeticCalculator.mul(1, 3);
//        System.out.println("---");
//        atithmeticCalculator.div(10, 2);
//        System.out.println("---");
        
        
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        //強制的類型使用接口的類型
        AtithmeticCalculator atithmeticCalculator = (AtithmeticCalculator) ctx.getBean(AtithmeticCalculator.class);
        
        int res = atithmeticCalculator.add(3, 6);
        System.out.println("res:" + res);
        System.out.println("-----");
//        int res1 = atithmeticCalculator.mul(2, 3);
//        System.out.println("res1:" + res1);
        
        //異常代碼的測試
//        int res2 = atithmeticCalculator.div(10, 0);
//        System.out.println("res2:" + res2);
    }

The method add begins with [3, 6]
--->beforeMethod1
The method add begins with [3, 6]
--->beforeMethod1
res:9
-----

註:

1.配置bean,實現aop的類

2.配置切面的bean

3.配置aop需要使用<aop:config>標簽

4.使用<aop:pointcut expression="execution(* com.MrChengsc.AOP.xml.AtithmeticCalculator.*(int, int))" id="pointcut"/>

  配置切點表達式

  *:代表任意的

  兩個int:可以使用 .. 進行替換

5.配置切面以及通知使用<aop:aspect>

  ref:引用已配置的切面類bean

  order:切面的優先級(數值越小優先級越大)

6.標簽:

<aop:before method="beforeMethod" pointcut-ref="pointcut"/> :前置通知
<aop:after method=""/>:後置通知
<aop:after-returning method="" returning="" pointcut="">:執行成功拿返回值
<aop:around method=""/>:環繞通知
<aop:after-throwing method="" >:異常通知

屬性:

pointcut-ref:引用切點表達式

method:切面類中的方法

returning:接受返回值

pointcut:切點表達式

7.spring:SpringAOP(配置文件)