AspectJ AOP例子

最近在學習Spring AOP,其中涉及到AspectJ的AOP框架。主要參考:http://howtodoinjava.com/spring/spring-aop/spring-aop-aspectj-example-tutorial-using-annotation-config/html

在編寫例子以前,先熟悉幾個spring AOP中專業術語:java

1. advice,通知,就是在方法以前或者以後你須要作的事情,好比日誌記錄,事務之類的等等。spring

2. pointCut,切入點,主要一系列表達式app

3. join points,鏈接點,是爲了切入點而定義出來的,pointCut對應的一系列方法框架

4. aspect, 切面, advice+pointCut,能夠肯定何時(before, after, around)作事情,在哪一個點(pointCut)作jvm

在spring中主要使用代理來包裹切面,把他們織入spring管理的bean中。其中有兩個方法:學習

1. 和目標類實現相同的接口測試

2. 繼承目標類代理

這兩種方式均可以經過jvm的檢查,在執行具體的功能時仍是由目標類完成,假裝類能夠在此以前作一些其餘的事情。日誌

接下來是一個AspectJ的小例子。

首先,建立一個切面,使用@Aspect註解

@Aspect
public class EmployeeCRUDAspect {
     
    @Before("execution(public * howtodoinjava.com.manager.EmployeeManager.*(..))")
    public void logBeforeV1(JoinPoint joinPoint) 
    {
        System.out.println("EmployeeCRUDAspect.logBeforeV1() : " + joinPoint.getSignature().getName());
    }
     

而後,對aop進行配置,applicationContext.xml文件的內容是:

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

<!-- aop命名空間 -->        
<aop:aspectj-autoproxy />
 
<context:component-scan base-package="howtodoinjava.com" />
 
<bean id="loggingAspect"  class="howtodoinjava.com.aspect.EmployeeCRUDAspect" />

</beans>

其餘輔助類有EmployeeDTO.java和EmployeeManager.java

---EmployeeDTO.java

package howtodoinjava.com.manager;

import java.util.ArrayList;
import java.util.List;

import howtodoinjava.com.dto.EmployeeDTO;

import org.springframework.stereotype.Component;

@Component
public class EmployeeManager
{
public EmployeeDTO getEmployeeById(Integer employeeId) {
System.out.println("Method getEmployeeById() called");
return new EmployeeDTO();
}

public List<EmployeeDTO> getAllEmployee() {
System.out.println("Method getAllEmployee() called");
return new ArrayList<EmployeeDTO>();
}

public void createEmployee(EmployeeDTO employee) {
System.out.println("Method createEmployee() called");
}

public void deleteEmployee(Integer employeeId) {
System.out.println("Method deleteEmployee() called");
}

public void updateEmployee(EmployeeDTO employee) {
System.out.println("Method updateEmployee() called");
}
}

  ----EmloyeeManager.java

package howtodoinjava.com.manager;

import java.util.ArrayList;
import java.util.List;

import howtodoinjava.com.dto.EmployeeDTO;

import org.springframework.stereotype.Component;

@Component
public class EmployeeManager 
{
    public EmployeeDTO getEmployeeById(Integer employeeId) {
        System.out.println("Method getEmployeeById() called");
        return new EmployeeDTO();
    }
 
    public List<EmployeeDTO> getAllEmployee() {
        System.out.println("Method getAllEmployee() called");
        return new ArrayList<EmployeeDTO>();
    }
 
    public void createEmployee(EmployeeDTO employee) {
        System.out.println("Method createEmployee() called");
    }
 
    public void deleteEmployee(Integer employeeId) {
        System.out.println("Method deleteEmployee() called");
    }
 
    public void updateEmployee(EmployeeDTO employee) {
        System.out.println("Method updateEmployee() called");
    }
}

  構建完成以後,建立測試類TestAOP.java

public class TestAOP
{
    @SuppressWarnings("resource")
    public static void main(String[] args) {
 
        ApplicationContext context = new ClassPathXmlApplicationContext("howtodoinjava/com/aspect/applicationContext.xml");
        EmployeeManager manager = context.getBean(EmployeeManager.class);
 
        manager.getEmployeeById(1);
        manager.createEmployee(new EmployeeDTO());
    }
}

  執行結果以下:

EmployeeCRUDAspect.logBeforeV1() : getEmployeeByIdMethod getEmployeeById() calledEmployeeCRUDAspect.logBeforeV1() : createEmployeeMethod createEmployee() called

相關文章
相關標籤/搜索