Spring基於註解配置AOP


D:\Java\IdeaProjects\JavaProj\SpringHelloWorld\src\aop.xmljava

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--配置自動掃描的包-->
    <context:component-scan base-package="com.xiya.spring.aop"/>
    <!--使AspectJ註釋起做用:自動爲匹配的類生成代理對象-->
    <aop:aspectj-autoproxy/>
</beans>

<context:component-scan base-package="com.xiya.spring.aop"/>
在xml配置了這個標籤後,spring能夠自動去掃描base-pack下面或者子包下面的Java文件,

若是掃描到有@Component @Controller@Service等這些註解的類,則把這些類註冊爲bean

spring

com/xiya/spring/aop/advice/LoggingAspect.java
ide

package com.xiya.spring.aop.advice;

/**
 * Created by N3verL4nd on 2017/3/24.
 */

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/**
 * 如何把這個類聲明爲一個切面:
 * 一、把該類放入IOC容器中
 * 二、再聲明爲一個切面
 */
@Aspect
@Component
public class LoggingAspect {
    //聲明該方法是一個前置通知:在目標方法執行以前執行
    @Before("execution(public int com.xiya.spring.aop.service.impl.ArithmeticCalculatorImpl.*(int,int))")
    public void doBefore(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();
        List<Object> args = Arrays.asList(joinPoint.getArgs());
        System.out.println("The method " + methodName + " begins with " + args);

    }

    //後置通知:在目標方法執行後(不管是否發生異常)執行的通知
    //在後置通知中還不能訪問目標方法執行的結果
    @After("execution(* com.xiya.spring.aop.service.impl.ArithmeticCalculatorImpl.*(int,int))")
    public void doAfter(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();
        System.out.println("the method " + methodName + " ends!");
    }
}
com/xiya/spring/aop/service/ArithmeticCalculator.java
package com.xiya.spring.aop.service;

/**
 * Created by N3verL4nd on 2017/3/24.
 */
public interface ArithmeticCalculator {
    int add(int x, int y);
    int sub(int x, int y);
    int mul(int x, int y);
    int div(int x, int y);
}
com/xiya/spring/aop/service/impl/ArithmeticCalculatorImpl.java
package com.xiya.spring.aop.service.impl;

import com.xiya.spring.aop.service.ArithmeticCalculator;
import org.springframework.stereotype.Component;

/**
 * Created by N3verL4nd on 2017/3/24.
 */
@Component
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
    @Override
    public int add(int x, int y) {
        int result = x + y;
        //int z = 1 / 0;
        return result;
    }

    @Override
    public int sub(int x, int y) {
        int result = x - y;
        return result;
    }

    @Override
    public int mul(int x, int y) {
        int result = x * y;
        return result;
    }

    @Override
    public int div(int x, int y) {
        int result = x / y;
        return result;
    }
}
com/xiya/spring/aop/test/Main.java
package com.xiya.spring.aop.test;

import com.xiya.spring.aop.service.ArithmeticCalculator;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by N3verL4nd on 2017/3/24.
 *
 */
public class Main {
    public static void main(String[] args) {
        //一、建立Spring的IOC容器
        ApplicationContext context = new ClassPathXmlApplicationContext("aop.xml");

        //二、從IOC容器中獲取bean實例
        ArithmeticCalculator calculator = context.getBean(ArithmeticCalculator.class);

        //三、使用bean
        int result = calculator.add(10, 20);
        System.out.println("result = " + result);

        result = calculator.div(10, 2);
        System.out.println("result = " + result);
    }
}
相關文章
相關標籤/搜索